Hacker News new | ask | show | jobs
by i_am_programmer 1671 days ago
This kinda reads as the writer doesn't actually know Python all that well, these definitely aren't Python antipatterns.

Lists and Sets are not the same data structure, obviously Sets are faster for finding if an item exists or not, but Sets are immutable and you can't have duplicate values like in a list. You can't substitue Sets for Lists for everything. What if I'm iterating over a list where I need to keep duplicates?

Also, who wraps a try except over a dictionary? Just use .get(), it defaults to None instead of raising a KeyError and even let's you pass a default value as the 2nd argument, .get("a", "hello").

The most basic google search could have helped with these.

2 comments

1. I wasn't intending to make it sound like you should substitute all your Lists to Sets as I am well aware of the differences between them. I was trying to convey that on some specific cases Sets are faster than lists, whether if it's obvious or not to everyone, I don't know.

2. Yes, you can use .get(), I was trying to demonstrate the idea of EAFP, and thought that the dictionary snippet might serve as a good example, we probably disagree.

nitpicky: sets aren't immutable. frozensets are.