Photo by AltumCode on Unsplash
Python is famous for its simplicity and ease of use. This is achieved, among other things, due to the compactness o...
For further actions, you may consider blocking this person and/or reporting abuse
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.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)
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
This comment is very critical, but is not meant to be hostile.
The objective should be clarity, not brevity. Writing shorter code is good only to the extent that it makes the code more readable. "Tricks" in general tend to be obscure, make the writer feel clever, and confuse the reader. Even something like slicing's step parameter should often be explained when it's used.
I recommend people learning python read Google's python style guide to understand the reasoning, not necessarily to follow. They would not encourage a nested comprehension for product. google.github.io/styleguide/pyguid...
"a real Python professional will only laugh at you." The passages like this bother me.
Comprehensions are not dogmatically correct, and someone laughing at a beginner for not using advanced features should not be the face of "real python professionals".
With that out of the way, I think these are useful things to know.
In general I'd advise someone to learn by googling what they're trying to do (eg python remove duplicates from list), look at top answers, and consider what is clearest.
So agree. I read this article with only thought - those explicit code, which author claims as junior's often is much more readable than suggested oneliners. And claims like "a real competent Python developer code" e.t.c are controversial for me. My experience reading code of a real highly experienced and clever professionals tells me that they usually tend to the simplest and most explicit solution possible. Their code is easy to read and understand. But tricky things usually appear because of ego and lack of self confidence as a professional. Real professional developer visible by his patterns, architecture and code style, not by some tricks.
If you replace the nested for loops (plain or in a generator expression) with a call to
itertools.product
, the reader will immediately see the intent.Most of the times, this carthesian product is only iterated over and above example will do. If it needs to be stored, you'll need a tuple:
Notice how each added level requires only an added argument to
product
.In a post for beginners it’s probably worth mentioning that sets are unordered. If you use a set to remove duplicates you can no longer index the data. Converting back to a list may not keep the original order either (? unsure about this).
You are absolutely right. It may be worth adding the following
The set does not contain duplicate elements;
The elements of the set are immutable (they cannot be changed), but the set itself is changeable, and it can be changed;
Since the elements are not indexed, the sets do not support any slicing and indexing operations.
I am infinitely glad that I can help anyone) Now my friends and I want to create a training course for guys like you. Give you information from experience and practice with homework and supervision. So far, our plan is to do it for free, so that we can learn by ourselves. If it will be interesting, then I left my contacts under the article. Connect with us we want to help you
Здравствуйте) для моей идеи вы просто золото) Правда я пока не совсем сформировал конечную задумку но я с удовольствием с вами посотрудничаю. Так как анлийский язык у меня только для чтения документации подходит)
Как вам удобно) быть может вы мне тоже поможете сформировать конечную идею)
напишите мне в дискорд если не сложно vadimkolobanov#5875
Love this post, it will surely help me.
I am very happy about it! If my work helped at least 1 person, then it was not in vain and my goal was achieved)
Removing duplicate in case insensitive list.
eg fruits=['banana', 'Banana', 'Mango', 'mango', 'orange', 'OranGe']
set(map(str.lower, fruits))
will give you: {'orange', 'banana', 'mango'}
It's very simple) You are trying to change the values of the set. You forget that the values in it are immutable, so the lower case will not work. In this situation, it is necessary to bring to one register earlier
Right, but set is applied right after we lower the case for all items.
demo: online-python.com/isJQnjgrZf
Nice. But I personally prefer the beginner versions.
It seems to me that all novice developers know about generators, since they are used in all tutorials, and you will not knowingly know how to use them.
if you look at the projects on github, you can see that there are almost never such solutions or generators in the principle. I mean newbie accounts with 1 repository. They know but they don't use it