This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Removing from a list while iterating over it [duplicate]
(5 answers)
Closed 5 years ago.
I have the next piece of code which counts unique elements in list and remove those elements from that list:
l = [1, 2, 3, 4, 5]
for i in l:
if l.count(i) == 1:
l.remove(i)
print l
However, after list modification by l.remove(i) I still have elements in the list: [2, 4]
My question is why list.remove() operation doesn't delete all elements if all of them are unique and amount per each one will be 1 during each iteration?
Related
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 3 years ago.
I am trying to remove duplicate elements from a list. The code is removing the wrong elements.
def simplify(A):
for i in A:
for j in A:
if i == j:
A.remove(j)
return A
Input A:
A = [1, 2, 5, 8, 12, 5]
Output:
A = [2, 8]
Try this.
def RemoveDuplicates(list_):
return list(set(list_))
You can call this function by passing your list and you get back listwithout duplicates.
This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Deleting multiple elements from a list
(32 answers)
Closed 4 years ago.
I have a list of values for example:
[1,2,3,4,5,6,7,8,9,10]
And specific indexes like:
[0,3,5]
I want something that returns deleting the values which the index belongs to the [0,3,5] array:
[2,3,5,7,8,9,10]
Any ideas for Python 3?
Thanks
Using enumerate and list comprehension.
Ex:
l = [1,2,3,4,5,6,7,8,9,10]
toDelete = [0,3,5]
print([v for i,v in enumerate(l) if i not in toDelete])
Output:
[2, 3, 5, 7, 8, 9, 10]
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 5 years ago.
a = [1, 2, 3, 4, 5]
for i in a:
a.remove(i)
print(a)
results : [2, 4]
I read a post about modifying list while iterating, and then I tried the code above, the result seems a little confusing.
Why the results here is [2, 4]?
You can't modify the collection you are iterating on. This will do the trick:
for element in lst[:] :
lst.remove(element)
This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 6 years ago.
I'm stuck with a part in one of my codes where I have to delete all the occurances present in listA that are identical in listB.
Example:
A=[1,4,4,4,3,3,2,1,5,5]
B=[4,3]
Result should be A=[1,2,1,5,5]. Ideally I would want to do it in linear time.
using Set Operations:
list(set(A) - set(B))
Using List Comprehension
list(set([i for i in A if i not in B]))
Try with list comprehension,
In [11]: [i for i in A if i not in B]
Out[11]: [1, 2, 1, 5, 5]
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Closed 7 years ago.
I have a list of 1000 elements. How do I access all of them starting from last element to the first one in a loop.
list = [4,3,2,1]
for item in list:
print(from last to first)
output = [1,2,3,4]
Try:
list = [4,3,2,1]
print [x for x in list[::-1]]
Output:
[1, 2, 3, 4]
list = [4,3,2,1]
print [i for i in list[::-1]]
Which gives the desired [1, 2, 3, 4]