convert string of list to list python code example

Example 1: 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 2: convert string to list python

# To split the string at every character use the list() function
word = 'abc'
L = list(word)
L
# Output:
# ['a', 'b', 'c']

# To split the string at a specific character use the split() function
word = 'a,b,c'
L = word.split(',')
L
# Output:
# ['a', 'b', 'c']

Example 3: python convert string to list

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

Example 4: python convert list to list of strings

# Basic syntax using list comprehension:
lsit_of_strings = [str(i) for i in your_list]

# Example usage:
your_list = ['strings', 'and', 'numbers', 11, 23, 42]
lsit_of_strings = [str(i) for i in your_list]
print(lsit_of_strings)
--> ['strings', 'and', 'numbers', '11', '23', '42'] # List of strings

Example 5: string to list python

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

Example 6: how to convert a string to a list python

#How to split a string into a list (Python)

#"separator" should be replaced with the string you want to split with
string.split("separator")