1

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!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MartyMcfly
  • 60
  • 5
  • can you post your data, i am thinking it can be done at that level – iamklaus Aug 01 '19 at 04:51
  • The result you can easily get with `[l0[0].replace("'", "").split(',') for l0 in l]` with `l` being your input list. What do you want to use `pandas` for? A further question to clarify would be: is the list nesting you want to entangle always that simple? – FObersteiner Aug 01 '19 at 06:42

5 Answers5

1

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']]

Yuan
  • 462
  • 4
  • 12
0

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']]
YusufUMS
  • 1,506
  • 1
  • 12
  • 24
0
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']]
tawab_shakeel
  • 3,701
  • 10
  • 26
0
  1. create an empty list a2
  2. iterate the lists a (containing l elemnts aka lists)
  3. create a3 empty list
  4. each l element of a has 1 item, a string (so... l[0] gets it)
  5. split that string l[0] by the ',' creating a new temporary list
  6. iterate this sublist, replace the "\'" and store in a3 the resulting items
  7. at the end of the loop of l[0].split(",") store a3 list into a2 as sublist
  8. 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']]

enter image description here

PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
0

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']]
Krishna
  • 6,107
  • 2
  • 40
  • 43
  • eval: This is very powerful, but is also very dangerous if you accept strings to evaluate from untrusted input. Suppose the string being evaluated is "os.system('rm -rf /')" ? It will really start deleting all the files on your computer. Check the link: https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval – Yuan Aug 01 '19 at 06:42
  • yes. I forgot again. will definitely use cautiously next time. – Krishna Aug 01 '19 at 07:01