Posts: 5
Threads: 2
Joined: Jun 2019
Jun-14-2019, 03:13 PM
(This post was last modified: Jun-14-2019, 03:28 PM by Yoriz.)
hi, there! i am new to python and i was trying to remove duplicate numbers in a list using for loop. but the code doesn't quite work
numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
for x in numbers:
if numbers.count(x) > 1:
numbers.remove(x)
print(numbers) it doesn't remove all the duplicates.
this is what i get when i run it.
Output: [3, 6, 6, 5, 10, 7, 19, 20]
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jun-14-2019, 03:46 PM
(This post was last modified: Jun-14-2019, 03:46 PM by metulburr.)
>>> numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
>>> list(set(numbers))
[3, 5, 6, 7, 10, 19, 20] or
>>> from collections import OrderedDict
>>> numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
>>> list(OrderedDict.fromkeys(numbers))
[3, 20, 6, 10, 5, 7, 19]
Recommended Tutorials:
Posts: 360
Threads: 5
Joined: Jun 2019
@ calonia Modifying a list whilst iterating over it is a very bad idea.
The 2nd example from @ metulburr is perfect as it keeps the order of the items in the original list.
If that doesn´t matter his 1st example is faster.
In addition your runtime is O(len(numbers)*len(numbers)) which is bad.
numbers.count(x) runs over the full list and counts each number.
Try to do this in O(len(numbers) which is possible. (not using above examples) :-)
Posts: 7,324
Threads: 123
Joined: Sep 2016
Also from 3.6 and guaranteed in 3.7 so are dictionaries ordered.
This mean that can drop import of OrderedDict,and just use dict() .
>>> numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
>>> list(dict.fromkeys(numbers))
[3, 20, 6, 10, 5, 7, 19]
Posts: 5
Threads: 2
Joined: Jun 2019
Jun-14-2019, 05:03 PM
(This post was last modified: Jun-14-2019, 05:03 PM by calonia.)
(Jun-14-2019, 04:10 PM)ThomasL Wrote: @calonia Modifying a list whilst iterating over it is a very bad idea.
The 2nd example from @metulburr is perfect as it keeps the order of the items in the original list.
If that doesn´t matter his 1st example is faster.
In addition your runtime is O(len(numbers)*len(numbers)) which is bad.
numbers.count(x) runs over the full list and counts each number.
Try to do this in O(len(numbers) which is possible. (not using above examples) :-)
thanks for the reply. i really appreciate your help. but i want to know what is wrong with this code. it doesn't seem to work with some of the numbers!
Posts: 360
Threads: 5
Joined: Jun 2019
Posts: 5
Threads: 2
Joined: Jun 2019
(Jun-14-2019, 05:13 PM)ThomasL Wrote: Have a read here
thank you very much. that was so helpful. i now get it. modifying a list while iterating through it(adding or removing items) leads to skipping indexes.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Printing is 'cheapest' debugging tool. If one adds two print statements everything is quite self-explanatory:
>>> numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
>>> for x in numbers:
... print(f'x is {x}')
... if numbers.count(x) > 1:
... numbers.remove(x)
... print(numbers)
...
x is 3 # first element in numbers
[3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
x is 20 # second element in numbers
[3, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
x is 10 # third element in numbers
[3, 6, 6, 5, 5, 7, 10, 7, 19, 19, 20]
x is 5 # fourth element in numbers
[3, 6, 6, 5, 7, 10, 7, 19, 19, 20]
x is 7 # fifth element in numbers
[3, 6, 6, 5, 10, 7, 19, 19, 20]
x is 7 # sixth element in numbers
x is 19 # seventh element in numbers
[3, 6, 6, 5, 10, 7, 19, 20]
x is 20 # eight and last element in numbers
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 5
Threads: 2
Joined: Jun 2019
(Jun-14-2019, 08:41 PM)perfringo Wrote: Printing is 'cheapest' debugging tool. If one adds two print statements everything is quite self-explanatory:
>>> numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
>>> for x in numbers:
... print(f'x is {x}')
... if numbers.count(x) > 1:
... numbers.remove(x)
... print(numbers)
...
x is 3 # first element in numbers
[3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
x is 20 # second element in numbers
[3, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
x is 10 # third element in numbers
[3, 6, 6, 5, 5, 7, 10, 7, 19, 19, 20]
x is 5 # fourth element in numbers
[3, 6, 6, 5, 7, 10, 7, 19, 19, 20]
x is 7 # fifth element in numbers
[3, 6, 6, 5, 10, 7, 19, 19, 20]
x is 7 # sixth element in numbers
x is 19 # seventh element in numbers
[3, 6, 6, 5, 10, 7, 19, 20]
x is 20 # eight and last element in numbers
thanks, that further illustrates my problem.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jun-14-2019, 09:13 PM
(This post was last modified: Jun-14-2019, 09:14 PM by metulburr.)
(Jun-14-2019, 06:50 PM)calonia Wrote: modifying a list while iterating through it(adding or removing items) leads to skipping indexes. You should know that you can loop over a copy of a list however
The [:] returns a shallow copy of the list, in which this case doesnt matter because all of the elements are ints
numbers = [3, 3, 20, 6, 10, 6, 5, 5, 7, 10, 7, 19, 19, 20]
for x in numbers[:]:
if numbers.count(x) > 1:
numbers.remove(x)
print(numbers) Output: [3, 6, 5, 10, 7, 19, 20]
Recommended Tutorials:
|