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']
Related
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
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]
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')])
This question already has answers here:
Removing from a list while iterating over it [duplicate]
(5 answers)
Closed 6 years ago.
In a list, I have duplicate elements that I want to remove.
The following code doesn't work:
Note:
temp containts the list of indexes of elements that I want to remove.
x is my list.
temp = self.list_duplicates(x)
for index in tmp:
del x[index]
Build a new list with a comprehension:
x = [element for (i,element) in enumerate(x) if i not in temp]
If you want to remove only duplicates, i.e. leaving one copy of the original, there is a better way to do that:
from collections import OrderedDict
x = list(OrderedDict.fromkeys(x))
x.pop(index) will remove the item at index. However, x = [x[i] for i in range(len(x)) if x[i] not in x[:i]] will remove the duplicates more quickly.
No one addressed the first part of the question. So, for removing duplicate items its wise to use set(), it removes the duplicate items and returns a arbitrary list of unique items.
lst = [1,1,2,3,4,4]
new_lst = list(set(lst))
print(lst)
It will return unique list of elements in an arbitrary manner Eg : [1,4,2,3]
You can filter the list with the following:
Edit: Works now for list of indices
x = list(filter(lambda item: x.index(item) not in temp, x))
This question already has answers here:
How to find all occurrences of an element in a list
(18 answers)
Closed 10 years ago.
I have the following list:
lista = [1,2,3,5,0,5,6,0]
I know that print(lista.index(0)) will print the first index where it found the number i.e. 4
How do I get it to print the next index which should be 7 etc?
The classic way is to build a list of the indices:
eg:
>>> indices = [i for i, v in enumerate(a) if v == 0]
>>> print indices
[4, 7]
Of course - this can be turned into a generator expression instead. But in short - using enumerate is what you're after.