There are errors in the part about removing duplicates.
fruits = { ... } already is a set, and so the duplicates are already discarded before reaching the loop.
The output of
new = set(fruits)
is not
['banana', 'apple', 'pear', 'orange']
but (order may differ)
{'banana', 'apple', 'pear', 'orange'}
The output you show would be obtained by
new = list(set(fruits))
Overall, to be correct, the example would have to be changed to
# List instead of set
fruits = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
# Actually make `new` a list
new = list(set(fruits))
Additionally, for a beginner post it would be very useful to point out that, in addition to be more concise,
new = list(set(fruits))
has O(N) time complexity[1], while
for fruit in fruits:
if fruit not in new:
new.append(fruit)
has O(N²) time complexity.
[1] In CPython anyway, which should be using a hash-table based set implementation. Note sure though, whether the loop or the set based solution is faster for small numbers of elements.
Thank you so much for the addition. Initially, my goal was just to show the difference in solutions to some problems. I will definitely add your suggestion about algorithmic complexity! And you also gave me the idea to write an article about calculating algorithmic complexity
Guys, I pointed out your help in the article and asked you to pay attention to the comments) So thank you very much! Many opinions, and various practices make this article even better)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There are errors in the part about removing duplicates.
fruits = { ... }
already is a set, and so the duplicates are already discarded before reaching the loop.The output of
is not
but (order may differ)
The output you show would be obtained by
Overall, to be correct, the example would have to be changed to
Additionally, for a beginner post it would be very useful to point out that, in addition to be more concise,
has O(N) time complexity[1], while
has O(N²) time complexity.
[1] In CPython anyway, which should be using a hash-table based set implementation. Note sure though, whether the loop or the
set
based solution is faster for small numbers of elements.Thank you so much for the addition. Initially, my goal was just to show the difference in solutions to some problems. I will definitely add your suggestion about algorithmic complexity! And you also gave me the idea to write an article about calculating algorithmic complexity
Under Python 3.6 and higher this one liner will also maintain the order
new = list(dict.fromkeys(fruits))
and it is O(n)!
Thank you all so much) You really help all readers to become a little more literate
Interesting to know, but hard to read :/ If I'd see this in the code I wouldn't be aware, that preserving the order is an intent here.
Just adding a comment would help.
Guys, I pointed out your help in the article and asked you to pay attention to the comments) So thank you very much! Many opinions, and various practices make this article even better)