This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
Currently I have a list of lists
l=[['asd'],['fgd']].
I want to turn it into a list
goal_list=['asd','fgd']
but I do not know how to turn a list into the value inside it - does anyone have an idea how to this efficiently?
This is a perfect use-case for itertools.chain, where we essentially chain all sublists together to flatten them!
from itertools import chain
def single_list(arr):
return list(chain(*arr))
print(single_list([['asd'],['fgd']]))
print(single_list([['asd', 'abcd'],['fgd', 'def']]))
The output will be
['asd', 'fgd']
['asd', 'abcd', 'fgd', 'def']
Just use a list comprehension with two loops:
goal_list = [item for sublist in l for item in sublist]
Related
This question already has answers here:
A quick way to return list without a specific element in Python
(9 answers)
Closed 5 years ago.
I have two lists, list1 and list2, I want to get all elements that are present in list1 but not in list2. Currently, I am typecasting lists into set and subtracting them and then again typecasting result to list.
list(set(list1) - set(list2));
But this is not performance efficient. Can you please suggest me alternate ways to do the same thing?
Using a list comprehension is probably simplest:
[x for x in list1 if not x in list2]
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 7 years ago.
Assume you have a list :
mylist=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
any pythonic(2.x) way to unpack the inner lists so that new list should look like ?:
mylist_n=[1,2,3,4,2,3,4,5,3,4,5,6]
import itertools
mylist=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
print list(itertools.chain(*mylist))
mylist_n = [j for i in mylist for j in i]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 7 years ago.
I have a different case of list Flattening.
Suppose I have a list of list ,say l=[['a'],[['p','l']]] . I simply want ['a',['p','l']]. That is the single elements should not be in nested lists and the lists having more than one element can be nested twice.
Thanks
Seems like you want something like this
In [4]: l=[['a'],[['p','l']]]
In [5]: [j for i in l for j in i]
Out[5]: ['a', ['p', 'l']]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 10 years ago.
I have an array of array in python. What is the best way to convert it to an array in python?
for example:
m = [[1,2],[3,4]]
# convert to [1,2,3,4]
I am new in python, so I dont know any solution of it better than writing a loop. Please help.
Use itertools.chain or list comprehension:
from itertools import chain
list(chain(*m)) # shortest
# or:
list(chain.from_iterable(m)) # more efficient
For smaller lists comprehension is faster, for longer ones chain.from_iterable is more suitable.
[item for subl in m for item in subl]
For understanding the nested comprehension, you can split it across multiple lines and compare it to a regular for loop:
[item #result = []
for subl in m #for subl in m:
for item in subl] # for item in subl:
# result.append(item)