Issue removing duplicates from list [duplicate] - python

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.

Related

Iterating over a list and removing elements after comparing them [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I'm trying to iterate over a list of number and removing the values that are lower than a number that I use to compare.
My problem is that there's a number that is lower than the value that I use but it doesnt get removed.
I'm using the remove() function of the list but I don't know why it doesn't get removed
Here is my code:
def remove_lower_numbers(array_numbers, bigger_number):
for elem in array_numbers:
if elem <= bigger_number:
array_numbers.remove(elem)
print(array_numbers)
It works if I used a list comprehension like this:
array_numbers = [x for x in array_numbers if x >= bigger_number]
but I want to do it the way I firts mentioned for learning purposes
I call the function like this:
cards_array = [3, 2, 7]
remove_lower_numbers(cards_array, 8)
but the function prints:
[2]
and 2 is lower than 8 it should return None or a empty list.
Using filter, which keeps only the values that return True for the lambda function:
list(filter(lambda x: x > 3, [1, 2, 3, 4, 5, 2, 3]))
Output:
[4, 5]

How to remove unwanted numbers from the list? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I have this below code written to remove numbers greater than 5 from the list.
But it does not work though the code looks fine to me. I'm new to python and trying to get my basics right. I decided to use remove since i'm checking based on the value
a = [1,5,4,6,3,8,9,5,9]
for i in a:
print (i)
if i<=5:
continue
a.remove(i)
print (a)
Result - [1, 5, 4, 3, 5, 9]
it will not work because you are removing item from list while iterating the same one....so when you remove an element, next iteration you will jump one one them.
Solution:
1) use 2 array;
2) use a while loop and when you remove an element you decrease the len of the array and you don't increment the iteration
a = [1,5,4,6,3,8,9,5,9]
i=0
l=len(a)
while i<l:
if a[i]>5:
a.pop(i)
l-=1
else:
i+=1
print (a)
Result:
[1, 5, 4, 3, 5]

Delete specific elements from a list in python 3 [duplicate]

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]

Modify item while iterating [duplicate]

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)

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?

Categories