This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 months ago.
I have a list that looks similar to:
list = [[[a,b,c], e, f, g], h, i, j]
and my desired output is:
merged_list = [a,b,c,e,f,g,h,i,j]
does anyone know an efficient way to do this?
i tried to do some sort of merging lists with the sum function but it didn't work
first i make all your variable into string because it was giving me error of not defined
so here is the code
#you have 3 list in total
list = [[['a','b','c'], 'e', 'f', 'g'], 'h', 'i', 'j']
output_list = [] # created a empty list for storing the all data in list
for list2 in list: # you will get 2 list now
for list1 in list2:# you will get 1 list now
for i in list1: # now you will only have items in list
output_list.append(i) #adding all item in output_list one by one
print(output_list) # printing the list
Related
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 months ago.
I have a list of lists each of those lists having one element. Is there a "pythonic" way to turn this into a list of elements that aren't lists outside of using the loop displayed below?
un_list = []
for x in home_times:
y=x[0]
un_list.append(y)
You can use list comprehension like this:
sample_input = [[1], [2], [3]] # list of lists having one element
output = [i[0] for i in sample_input]
And this is the output:
[1, 2, 3]
Read more about list comprehension here.
Another way, using sum function.
lst=[['5'], [3],['7'],['B'],[4]]
output= sum(lst, [])
print(output)
Output:
['5', 3, '7', 'B', 4]
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)
This question already has answers here:
How can I compare two lists in python and return matches [duplicate]
(21 answers)
Closed 1 year ago.
I would like to find the duplicated data from two different list. The result should show me which data is duplicated.
Here is the list of my data
List1 = ['a', 'e', 'i', 'o', 'u']
List2 = ['a', 'b', 'c', 'd', 'e']
The function
def list_contains(List1, List2):
# Iterate in the 1st list
for m in List1:
# Iterate in the 2nd list
for n in List2:
# if there is a match
if m == n:
return m
return m
list_contains(List1,List2)
I get the result is 'a' but the result I expected is 'a','e'.
set(List1).intersection(set(List2))
Issue with your code:
return statement will exit from the function, so at the first match it will just return with the first match. Since you need all the matches not just the first match you need to capture them into say a list and then return rather then returning at the fist match.
Fix:
def list_contains(List1, List2):
result = []
# Iterate in the 1st list
for m in List1:
# Iterate in the 2nd list
for n in List2:
# if there is a match
if m == n:
result += m
return result
This question already has answers here:
Python: filter list of list with another list
(2 answers)
Closed 5 years ago.
Say I have a list of strings, mylist, from which I want to extract elements that satisfy a condition in a another list, idx:
mylist = ['a','b','c','d']
idx = ['want','want','dont want','want']
What I want as output is:
['a','b','d']
which is a list of elements that I 'want'
How can this by done?
You can use zip to stride through your two lists element-wise, then keep the elements from mylist if the corresponding element from idx equals 'want'
>>> [i for i, j in zip(mylist, idx) if j == 'want']
['a', 'b', 'd']
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 5 years ago.
I have a list which consist of string and integers. I have to pop only integers and put that in a separate list. My code:
list1=['a','b','c','d','e','f','g','h',1,2,3]
list=[]
x=0
for i in list1:
if isinstance(i,int) :
list.append(i)
list1.pop(x)
x += 1
print(list1)
print(list)
Output of above code
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 2]
[1, 3]
My question is: Why are all the integers not removed by my code? What is wrong with my code?
You iterate and manipulate the same list at the same time:
for i in list1: # iteration
if isinstance(i,int):
list.append(i)
list1.pop(x) # manipulate
x += 1
This usually does not work: the for loop works with a cursor. If you meanwhile removes an item, you thus will start skipping elements.
It is better to simply use a declarative and Pythonic approach, like for instance the following:
list_int = [i for i in list1 if isinstance(i,int)]
list1 = [i for i in list1 if not isinstance(i,int)]
Furthermore you should not name a variable list, since then you remove the reference to the list class.