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)))
Related
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]
This question already has answers here:
Finding and replacing elements in a list
(10 answers)
Closed 4 years ago.
I would like to update an element from a list.
This is my kind of list:
list= ["toto", "stack" , "element_to_update", "overflow"]
I want to update this list when "toto" and "stack" found as element[0] and element[1] to have my list updated (and concatenate with existing element[2])
The list that I want at the end:
list=["toto", "stack", "element_to_update_is_updated", "overflow"]
What is the best way to do that?
Thanks in advance for your help.
Why not:
l = ["toto", "stack", "element_to_update", "overflow"]
if l[:2] == ['toto','stack']:
l[2] += '_is_updated'
And now:
print(l)
Is:
['toto', 'stack', 'element_to_update_is_updated', 'overflow']
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.
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]