python convert string list to list code example

Example 1: convert list to string python

# Python program to convert a list 
# to string using list comprehension 
   
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas'] 
  
# using list comprehension 
listToStr = ' '.join([str(elem) for elem in s]) 
  
print(listToStr)

Example 2: convert list to string python

my_list = ["Hello", 8, "World"]
string = " ".join(my_list)
print(string)
"""
output
Hello 8 World
"""

Example 3: string list into list pandas

import ast 

# initializing string representation of a list 
ini_list = "[1, 2, 3, 4, 5]"
  
# Converting string to list 
res = ast.literal_eval(ini_list) 
  
# printing final result and its type 
print ("final list", res) 
print (type(res))

Example 4: python convert string to list

fruit = 'apple oranges banans'
newlist = fruit.split()
print(newlist)

Example 5: string to list python

s = 'hello'
l = list(s)
print(l) # prints ['h', 'e', 'l', 'l', 'o']

Example 6: convert string to list python

word = "Hey Hello world"
print(list(word))
# output
# ["Hey", "Hello", "world"]