Python - unpack list of lists [duplicate] - python

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.
What is the cleanest way to implement the following:
list = [list1, list2, ...]
return [*list1, *list2, ...]
It looks like this syntax will be available in Python 3.5. In the meantime, what is your solution?

This is commonly written as:
[x for l in list for x in l]

Related

making sublist from sublist based on index position python [duplicate]

This question already has an answer here:
How to zip lists in a list [duplicate]
(1 answer)
Closed 3 years ago.
I have a list
mainlist=[[1,2,3,4],['a','b','c','d'],[4,5,6,7]]
want to make sublist based on index position in sublist. The output is
finallist=[[1,'a',4],[2,'b',5],[3,'c',6],[4,'d',7]]
Thanks
Use zip with unpacking:
finallist = list(map(list, zip(*mainlist)))

get elements that are present in list1 but not in list2 (like set operation A - B) [duplicate]

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]

How to convert a python list containing several lists into single list? [duplicate]

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]

unpacking lists inside another list python [duplicate]

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]

unpack lists of list into list with individual items [duplicate]

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))

Categories

Resources