Should You Use A List Or A Set? // Python Tips

preview_player
Показать описание


🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

I think the uniqueness of set items should also be a factor here, it's probably the most important

vldor
Автор

You actually changed the behavior of the function! (Looks like you introduced a bug)
If your review string is "super cool bad bad bad movie", the first version returns False and the new one (with set) returns True
It's because in the first version each "bad" will be counted, and in the second version you only count DISTINCT words.
That's why even for super simple functions we need unit tests :p
By the way, I really enjoy the quality of your videos and this the very first time I find a bug in it (I watched them all haha)

tamles
Автор

You can write your set operators as symbols, so & instead of .intersection()...
Also you can treat dict keys as sets. This is great for stuff like validating json/yaml. Ala:

if valid_keys == input.keys(): if all should be there, or
if valid_keys - input.keys() == set() if you want to say the only keys you want is valid_keys.

johnstoner
Автор

In my opinion you are using sets not to "make the code cleaner" but exactly because you need set operations like Union, Difference etc... In addition you would mainly use sets if you have unique elements and I don't care about the index of an element. It gives you the benefit of much faster "is in" operations compared to lists.

mrmivpushkin
Автор

This is actually an overlooked gem. I think I've done the for x in..way too many times

DistortedV
Автор

Also set lookup is fast vs. linearly scanning a list (esp. big lists) as set will use a form of hashing, like dicts.

xlerb_again_to_music
Автор

Love this and these shorts. Out of curiosity what's the space, time complexity compared to a for loop?

MrAjiii
Автор

I see why you need to consider the different data types for the appropriate application, but this wasn't a good example (when the main advantage mentioned was simpler/shorter code) because with list comprehension you could also do it in 2 rows.

Gummibandet
Автор

There is a set intersection operator &.
I believe that "a & b" looks much more simple (and clear) than "a.intersection(b)"

Ivan-Bagrintsev
Автор

The thing that bought home how important the right data structure was the backtracking solution to the 8 queen problem described by Horowitz and Sahni (Fundamentals of Computer Algorithms 1978 Page 337)

alecclews
Автор

cs]} to replace ] for } with vim bindings

Gokuroro
Автор

For python 2.7.17 (obsolete) the line `def detect_positive(review: str) -> bool:` needs to say `def detect_positive(review=""): # -> bool` because python 2 raises `SyntaxError: invalid syntax`. The `list` vs. `set` discussion applies to both python 2 and 3.

abilitytools
Автор

If you're using a Vim emulation plugin, you could have used cs]} in order to change the surrounding square brackets into parentheses, instead of doing it manually :)

agastyam_
Автор

Maybe using the filter() function for the list, easier

ayder
Автор

Fun fact, when iterating through a list to find if an item exists, it's generally more efficient to convert the list into a set and iterate through the set.

emiyox
Автор

Short videos is a very stupid format even if the content is great

oscarmartinezbeltran