pythonic way to flatten nested lists? [duplicate] - python

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Apply function to each element of a list
(4 answers)
Closed 6 months ago.
I have a list-tuple-list tree and I'd like to flatten it for a query, so that I can print all the items of the the first lists of each tuple.
I can do it via a for loop:
bigNest = [([item1,item2],[]),([item3],[item4])]
mergedlist = []
for listItem in bigNest:
mergedlist += listItem[0]
print mergedList
Is there a simpler/quicker way which will also work with larger tuples?

edit: sorry the last post was wrong, you could do a list comprehension:
mergedlist = []
foovar = [mergedlist.extend(i[0]) for i in bigNest]

Related

How to convert two list of lists into list of dictionary such that the first element comes from the first list and the second from the other list [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed last year.
Suppose I have two list of lists as follows:
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
How would I get the above as:
List3 = [{1:1.2,2:2.4},{3:2.4,4:5.0},{3:4.5,8:6.0}]
Try this:
List3 = [dict(zip(*lsts)) for lsts in zip(List1, List2)]
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
List3=[]
for i,e in enumerate(List1):
List3.append(dict(zip(List1[i],List2[i])))
print(List3)

Python. Removing list from another list [duplicate]

This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 1 year ago.
I have 2 lists
for i in range(1, 26):
U.append(i)
A = [8,9,10,11,12,16,17,22,25]
How can I extract A values from U list?
Use a list comprehension:
[item for item in U if item not in A]

remove special characters to get string in python list [duplicate]

This question already has answers here:
Get the first element of each tuple in a list in Python [duplicate]
(5 answers)
Closed 2 years ago.
I have the following code which gives me list as shown below
listP= methodcall()
print(listP)
Output [('abcd',), ('efgh',)
How do I remove bracket,comma and single quotes?
I need below output:-
listP=['abcd','efgh']
This will do the work:
listp = [('abcd',), ('efgh',)]
listp = [l[0] for l in listp]
print(listp)
Take only first element from the tuples in the original list using list comprehension.
newlist = []
list1 = [('abcd',), ('efgh',)]
for elements in list1:
newlist.append(elements[0])
print(newlist)

list comprehension even list elements instead of loop and how to refer only to the index [duplicate]

This question already has answers here:
Shortest way to slice even/odd lines from a python array?
(4 answers)
Closed 3 years ago.
I want to use list comp' instead of a loop.
Let's sat I have a list of lists and I want only the even index elements.
Eg: [[a,a], [b,b], [c,c],[g,g]] to..... [[a,a], [c,c]]
This doesn't work
a = [i for i in the_list if i % 2 == 0)]
Here is a possible solution that keeps only lists with even indexes:
result = [lst for i, lst in enumerate(the_list) if i % 2 == 0]

Adding sublists into one list as lists elements in python [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(35 answers)
Closed 4 years ago.
I'm looking for a way to sum up my output of list. I want to add each output of this for loop into one "big" list as a single list element.
for line in f.split('\n'):
words = line.split()
How do I achieve this:
L1 = [1,2,3]
L2 = [4,5,6]
L3 = [7,8,9]
SumList = [[1,2,3],[4,5,6],[7,8,9]]
You can append str.split list to another list
Ex:
res = []
for line in f.split('\n'):
res.append(line.split())
Or use list comprehension
Ex:
print([line.split() for line in f.split('\n')])

Categories