Change language

Python | Convert a string of dissimilar type to a list

|

Method # 1: Using List Comprehension + split () + strip ()
A combination of the above methods can be used to solve this problem. In this, we perform element separation and then strip the wildcard for data type conversion and compile all the list building logic using a list comprehension.

# Python3 demo code works
# Convert heterogeneous type string to list
# using list comprehension + split () + strip ()

 
# initialization string

test_str = "’ gfg’, ’is’, True,’ best’ , False "

  
# print original line

print ( "The original string is : " + test_str)

  
# Convert heterogeneous type string to list
# using list comprehension + split () + strip ()

res = [ele.strip ( ) if ele.strip (). startswith ( "’ " ) else ele = = True

for ele in test_str.split ( ’,’ )]

  
# print result

print ( "List after conversion from string:" + str (res))

Output:

 The original string is: ’gfg’,’ is’, True , ’best’, False List after conversion from string: [" ’gfg’", "’ is’ ", True," best’ ", False] 

Method # 2: Using eval()
This built-in function automatically detects the data type and performs the conversion. This is a one-phrase solution, and also a solution even if the integers are in a string and therefore recommended for this solution.

# Python3 code to demonstrate how it works
# Convert a heterogeneous type string to a list
# using eval ()

 
# initialization string

test_str = " ’gfg’,’ is’, True, ’best’, False, 1, 2"

 
# print original line

print ( "The original string is:" + test_ str)

 
# Convert heterogeneous type string to list
# using eval ()

res = list ( eval (test_str))

 
# print result

print ( "List after conversion from string:" + str (res))

Output:

 The original string is: ’gfg’,’ is’, True, ’best’, False, 1, 2 List after conversion from string: [" ’gfg’", "’ is’ ", True," best’ ", False, 1, 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

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

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

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

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

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


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

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

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

Python - Move item to the end of the list

Python - Print list vertically