tags:

views:

177

answers:

4

When should I use a Dictionary, List or Set?

Are there scenarios that are more suited for each collection?

+10  A: 
  • 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.
Jon Skeet
+4  A: 

When you want an unordered collection of unique elements, use a set. (For example, when you want a list of all the words used in a document).

When you want to collect an immutable ordered list of elements, use a tuple. (For example, when you want a pair of (name, phone number) that you wish to use as an element in a set. Sets require elements be immutable).

When you want to collect an mutable ordered list of elements, use a list. (For example, when you want a modifiable list of phone numbers: [number1, number2, ...]).

When you want a mapping from keys to values, use a dict. (For example, when you want a telephone book which maps names to phone numbers.) The keys in a dict are unordered. (If you iterate through a dict (telephone book), the keys (names) may show up in any order).

unutbu
Uh, he didn't ask about tuples.
Aaron Gallagher
+4  A: 
  • Use a dictionary when you have a set of unique keys that map to values.

  • Use a list if you have an ordered collection of items.

  • Use a set to store an unordered set of items.

SLaks
+12  A: 

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 with each key 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.

Alex Martelli
I wish I could donate my answer votes to yours... Hey everyone, this is where the meat is :)
Jon Skeet
I took the bait! thanks for the summary!
Blankman
@Jon, thanks! Hmmm, maybe the idea of "transferring upvotes" (at some "discount rate", e.g., 2 for 1) is worth discussing in meta...?-) I know I've felt similarly on certain occasions...
Alex Martelli