In Python, when to use a Dictionary, List or Set?
When should I use a dictionary, list or set?
When should I use a dictionary, list or set?
The python wiki says: “Membership testing with sets and dictionaries is much faster, O(1), than searching sequences, O(n). When testing “a in b”, b should be a set or dictionary instead of a list or tuple.”
I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value.
[] = empty list
Tested on Python 2.6 interpreter:
I have a list of filenames in python and I would want to construct a set out of all the filenames.
When you do something like "test" in a where a is a list does python do a sequential search on the list or does it create a hash table representation to optimize the lookup? In the application I need this for I’ll be doing a lot of lookups on the list so would it be best to do something like b = set(a) and then "test" in b? Also note that the list of values I’ll have won’t have duplicate data and I don’t actually care about the order it’s in; I just need to be able to check for the existence of a value.
here is the problem: I have a list of tuples (could be sets as well if needed). For instance:
The question arose when answering to another SO question (there).
I stumbled across a blog post detailing how to implement a powerset function in Python. So I went about trying my own way of doing it, and discovered that Python apparently cannot have a set of sets, since set is not hashable. This is irksome, since the definition of a powerset is that it is a set of sets, and I wanted to implement it using actual set operations.