Reputation: 60
I currently have a list which looks something like:
[["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
I'm hoping to get a results like:
[['MILK','EGGS','BUTTER','CHEESE'],['MILK', 'BUTTER', 'EGGS'],['EGGS', 'BUTTER']]
It was created from a single-column dataframe using:
value_list = value_df.values.tolist()
I was trying to remove the double quotes around the list to no-avail - I've tried accessing the sublist using something like:
for sublist in value_list:
sublist[:] = [s.strip('"') for s in sublist]
But it doesn't seem to be affecting those quotes pre-and-post content.
Any help would be greatly appreciated! Thank you!
Upvotes: 1
Views: 623
Reputation: 1493
You can try this simple approach:
list_a = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
list_b = []
for a in list_a:
for aa in a:
list_b.append(aa.replace("'","").split(","))
print(list_b)
Or more simpler code despite using the same approach:
list_a = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
list_b = [aa.replace("'","").split(",") for a in list_a for aa in a]
print(list_b)
If you run the code, the output will be like this:
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'], ['MILK', ' BUTTER', ' EGGS'], ['EGGS', ' BUTTER']]
Upvotes: 0
Reputation: 23443
- create an empty list a2
- iterate the lists a (containing l elemnts aka lists)
- create a3 empty list
- each l element of a has 1 item, a string (so... l[0] gets it)
- split that string l[0] by the ',' creating a new temporary list
- iterate this sublist, replace the "\'" and store in a3 the resulting items
- at the end of the loop of l[0].split(",") store a3 list into a2 as sublist
- at the end of the main loop you have the desired output
a = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
a2 = []
for l in a:
a3 = []
for sub in l[0].split(","):
sub = sub.replace("\'","")
a3.append(sub)
a2.append(a3)
>>> print(a2)
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'], ['MILK', ' BUTTER', ' EGGS'], ['EGGS', ' BUTTER']]
Upvotes: 0
Reputation: 7226
Following should do.
lst = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
[list(eval(i[0])) for i in lst]
Output:
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'],
['MILK', 'BUTTER', 'EGGS'],
['EGGS', 'BUTTER']]
Upvotes: 0
Reputation: 472
Anytime, when talking about removing quotation from string, the ast.literal_eval
comes to my mind. The lib was born to do the task.
import ast
l = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
newL = [list(ast.literal_eval(i[0])) for i in l]
print(newL)
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'],
['MILK', 'BUTTER', 'EGGS'],
['EGGS', 'BUTTER']]
Upvotes: 1
Reputation: 3739
abc = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
final_list=[]
[final_list.append(a.replace("'","").split(",")) for i in abc for a in i]
print(final_list)
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'], ['MILK', ' BUTTER', ' EGGS'], ['EGGS', ' BUTTER']]
Upvotes: 0