Change language

# Python | Convert a list of tuples of mixed data types to a list of strings

|

Method # 1: Using a comprehension list + ` tuple () + str () ` + expression generator
A combination of the above functions can be used to accomplish this task. In this, we extract each element of the tuple using a generation expression and perform the conversion using str (). Iterate for each tuple by comprehending the list.

 ` # Python3 code to demonstrate how it works ` ` # Convert mixed list of tuples to list of strings ` ` # use comprehension list + tuple () + str () + generator expression `   ` # initialize the list ` ` test_list ` ` = ` ` [(` ` ’gfg’ ` `, ` ` 1 ` `, ` ` True ` `), (` `’ is’ ` `, ` ` False ` `), (` ` ’best’ ` `, ` ` 2 ` `)] `   ` # print original list ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) ` ` `  ` # Convert mixed list of tuples to list of strings ` ` # use comprehension list + tuple () + str () + generator expression ` ` res ` ` = ` ` [` ` tuple ` ` (` ` str ` ` (ele) ` ` for ` ` ele ` ` in sub) for sub in test_list] ``   # print result print ( " The tuple list after conversion: " + str (res)) `

Output:

` The original list: [(’gfg’, 1, True), (’ is’, False), (’best’, 2)] The tuple list after conversion: [(’ gfg’, ’1’,’ True’), (’ is’, ’False’), (’ best’, ’2’)] `

Method # 2: Using ` map () + tuple () + str ( ) ` + List Comprehension
A combination of the above functions can be used to accomplish this task. In this we accomplish the task performed by the generator expression above using map ().

 ` # Python3 code to demonstrate how it works ` ` # Convert a mixed list of tuples to a list of strings ` ` # using map () + tuple () + str () + list comprehension `   ` # initialize the list ` ` test_list ` ` = ` ` [(` ` ’gfg’ ` `, ` ` 1 ` `, ` ` True ` `), (` ` ’is’ ` `, ` ` False ` `), (` ` ’best’ , 2 )] ``   # print original list print ( "The original list:" + str (test_list))   # Convert mixed list of tuples to list of strings # using map () + tuple () + str () + list comprehension res = [ tuple ( map ( str , sub)) for sub in test_list]    # print result print ( "The tuple list after conversion:" + str (res)) `

Output:

` The original list: [(’gfg’, 1, True), (’ is’, False), (’best’, 2)] The tuple list after conversion: [(’gfg’,’ 1’, ’True’), (’ is’, ’False’), (’ best’, ’2’)] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries