Modify item while iterating [duplicate] - python

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)

Related

How do I move items from one list to another? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 10 months ago.
I am trying to remove all the items from list2 into list1 like this:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
for item in l2:
l1.append(item)
l2.remove(item)
the problem is:
#l1 now returns [1, 2, 3, 4, 6]
#l2 now returns [5]
I want it so that:
#l1 returns [1, 2, 3, 4, 5, 6]
#l2 returns []
Why does this happen? And what is the conventional way to achieve what I'm trying to do, in Python?
Your code doesn't work because you are removing items while iterating.
For your problem - moving all items from one list to another list - the best solution is to use built-in methods provided by lists (the latter since Python 3.3):
l1.extend(l2)
l2.clear()
you can replace l2.clear() with l2 = [] if there are no other references to l2

Issue removing duplicates from list [duplicate]

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.

list.remove method doesn't work properly [duplicate]

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?

python remove elements from list based on other list [duplicate]

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]

How to access all list elements from last to first in python list [duplicate]

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]

Categories