This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
x = ['[1867, 1868]', '[6612663]']
Expected Output:
x = [[1867, 1868], [6612663]]
I tried ,
x = [item.replace("'", "") for item in x]
It didn't work.
Could someone help?
You can use ast.literal_eval with a list comprehension:
import ast
x = ['[1867, 1868]', '[6612663]']
new = [ast.literal_eval(ls) for ls in x]
print(new)
Output:
[[1867, 1868], [6612663]]
Related
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 2 years ago.
How to flatten the coordinates?
For input :[(1,2),(5,6),(7,8)]
expecting the output as [1,2,5,6,7,8]
Try it like this:
mylist = [(1,2),(5,6),(7,8)]
newlist = []
for x in mylist:
newlist += x
print(newlist)
from the itertools doc page
from itertools import chain
def flatten(listOfLists):
"Flatten one level of nesting"
return chain.from_iterable(listOfLists)
This question already has answers here:
Remove all occurrences of a value from a list?
(26 answers)
Closed 4 years ago.
How can I remove several same items in a LIST
for ex:
a = [1,1,1,1,1,1,2,2,2,2,2,2]
I want to remove all 1 values, such that output is:
a = [2,2,2,2,2,2]
I tried a.remove(1) but it only remove one '1' at the first encounter.
i try looking for comprehension method
Try it with a list comprehension:
a = [1,1,1,1,1,1,2,2,2,2,2,2]
cleaned_a = [el for el in a if el != 1]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 years ago.
How do I convert a list of lists to a single list?
Input:
a=[['AA'], ['AE'], ['AH'], ['AO'],]
Desired output:
['AA','AE','AH','AO']
a=[['AA'], ['AE'], ['AH'], ['AO'],]
l=[]
for i in a:
l.extend(i)
print l
you could use comprehension list:
or using map and lambda functions
a=[['AA'], ['AE'], ['AH'], ['AO'],]
# open the item [0] with generators
Newa = [x[0] for x in a]
>>>['AA', 'AE', 'AH', 'AO']
see examples: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
EDIT:
for i, value in enumerate(a):
a[i] = value[0]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 8 years ago.
I have a list like:
l = [[1,2,3],[4,5]]
I would like to unpack each element to make it like:
l = [1,2,3,4,5]
I have my solution here:
l = reduce(lambda x, y: x+y, l)
Anyone has other Pythonic way? Thanks.
You should use itertools methods;
from itertools import chain
l = [[1,2,3],[4,5]]
list(chain.from_iterable(l))
This question already has answers here:
Add a character to each item in a list [duplicate]
(3 answers)
Closed 9 years ago.
I have a string which I want to concatenate with every object within a list. Here is an example:
a = ['1','2']
b = 'a'
and I want:
c = ['a1','a2']
It seems that strings can't be concatenated to list objects directly so I assume that I should convert my list to the string and then add it. Is it correct or any suggestions?
Try Python list comprehensions.
>>> a = ['1','2']
>>> b = 'a'
>>> [b+i for i in a]
['a1', 'a2']