A list keeps order, dict and set don't: when you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course ;-) ).
dict associates each key with a value, while list and set just contain values: very different use cases, obviously.
set requires items to be hashable, list doesn't: if you have non-hashable items, therefore, you cannot use set and must instead use list.
set forbids duplicates, list does not: also a crucial distinction. (A "multiset", which maps duplicates into a different count for items present more than once, can be found in collections.Counter -- you could build one as a dict, if for some weird reason you couldn't import collections, or, in pre-2.7 Python as a collections.defaultdict(int), using the items as keys and the associated value as the count).
Checking for membership of a value in a set (or dict, for keys) is blazingly fast (taking about a constant, short time), while in a list it takes time proportional to the list's length in the average and worst cases. So, if you have hashable items, don't care either way about order or duplicates, and want speedy membership checking, set is better than list.
A list keeps order, dict and set don't: when you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course ;-) ).
dict associates each key with a value, while list and set just contain values: very different use cases, obviously.
set requires items to be hashable, list doesn't: if you have non-hashable items, therefore, you cannot use set and must instead use list.
set forbids duplicates, list does not: also a crucial distinction. (A "multiset", which maps duplicates into a different count for items present more than once, can be found in collections.Counter -- you could build one as a dict, if for some weird reason you couldn't import collections, or, in pre-2.7 Python as a collections.defaultdict(int), using the items as keys and the associated value as the count).
Checking for membership of a value in a set (or dict, for keys) is blazingly fast (taking about a constant, short time), while in a list it takes time proportional to the list's length in the average and worst cases. So, if you have hashable items, don't care either way about order or duplicates, and want speedy membership checking, set is better than list.
- Do you just need an ordered sequence of items? Go for a list.
- Do you just need to know whether or not you've already got a particular value, but without ordering (and you don't need to store duplicates)? Use a set.
- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a dictionary.
Hey everyone,
I’m trying to get a deeper understanding of Python’s built-in data structures—specifically lists, dictionaries, tuples, and sets. I understand their basic properties:
Lists (list) → Ordered, mutable, allows duplicates
Tuples (tuple) → Ordered, immutable, allows duplicates
Dictionaries (dict) → Key-value pairs, unordered (but ordered since Python 3.7), keys must be unique
Sets (set) → Unordered, mutable, unique elements only
But I'm struggling to figure out when to use each of them in real-world scenarios. I know some general guidelines, like:
Use lists when order matters and you need to modify elements.
Use tuples when you need an immutable sequence (e.g., function return values).
Use sets when you need fast lookups and uniqueness.
Use dictionaries when mapping keys to values efficiently.
However, I’d love to hear from experienced Python developers:
Are there any specific patterns or real-world examples where one of these structures is clearly the best choice?
Have you ever misused one and later realized another structure would have been better?
Are there any performance considerations that I should be aware of when choosing between them?
Looking forward to your insights! Thanks in advance.
When to use sets, tuples, lists, or disctionaries?
In Python, when to use a Dictionary, List or Set?
When should I use a list, dictionary, tuple, or set in Python?
Performance difference defaultdict versus dict.get(key,default)
I doubt your problem stems from dict.get(). As u/RubyPinch pointed out, the two alternatives are quite similar.
You're setting the recursion limit to a million, the default is a thousand I believe. So I assume you're working with lots of data.
self.leader[self.current_source] = self.leader.get(self.current_source,[]) + [node]
creates a copy of self.leader[self.current_source](if it exists) every time it's executed. If that list is big, you've got a problem.
self.leader[self.current_source] += [node]
modifies the list in place.
More on reddit.comWhen should I use a set instead of a list in Python?
Is a Python dictionary ordered?
What is the main difference between a Python list and a tuple?
I just got to the point where I know what all of these container types do, but I have a question. Ahen to use which ones?