Coder Perfect

When should you use a Dictionary, List, or Set in Python?

Problem

What is the difference between a dictionary, a list, and a set?

Are there scenarios that are more suited for each data type?

Asked by Blankman

Solution #1

A list maintains order, whereas dict and set do not: if you care about order, you must use list (unless your container options are restricted to these three, of course ;-)).

List and set only hold values, whereas dict associates each key with a value: obviously, they serve quite different purposes.

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.

A major point is that set prohibits duplicates whereas list does not. (Collections contain a “multiset,” which maps duplicates to a different count for items that appear more than once.) If you couldn’t import collections for some reason, you could make one as a dict, or as a collections in pre-2.7 Python. defaultdict(int), with the keys being the entries and the associated value being the count).

In the average and worst instances, checking for membership of a value in a set (or dict, for keys) is lightning fast (taking about a constant, brief time), whereas in a list, it takes time proportionate to the list’s length. Set is preferable than list if you have hashable objects, don’t care about order or duplicates, and want to check membership quickly.

Answered by Alex Martelli

Solution #2

Answered by Jon Skeet

Solution #3

Use a set to create an unordered group of unique objects. (For instance, if you want a list of all the terms in a document.)

When you want to collect an immutable ordered list of elements, use a tuple. (For example, when you want a (name, phone_number) pair that you wish to use as an element in a set, you would need a tuple rather than a list since sets require elements be immutable).

Use a list when you need to collect a mutable, sorted list of elements. (For example, [number1, number2,…] when you want to add new phone numbers to a list.)

Use a dict when you need a mapping from keys to values. (For example, if you need a phone book that maps names to phone numbers, type ‘John Smith’: ‘555-1212’.) It’s worth noting that the keys in a dict aren’t in any particular order. (Keys (names) may appear in any order while iterating through a dict (telephone book).

Answered by unutbu

Solution #4

Answered by SLaks

Solution #5

In short, use:

If you need an item to be in a specific order, use list.

If you need to link values to keys, use dict.

set – if you require to keep unique elements.

A list is a changeable sequence that is commonly used to hold groups of similar objects.

All of the common sequence operations are implemented in a list:

All of the mutable sequence operations are also implemented by a list:

The methods append and pop can be used to convert a list into a stack.

A dictionary is a collection of hashable values that are mapped to arbitrary objects. A changeable object is a dictionary. A dictionary’s fundamental activities are storing a value with a key and extracting the value from the key.

Values including lists, dictionaries, or other mutable types cannot be used as keys in a dictionary since they are not hashable.

A set is a collection of unique hashable objects that are not in any particular order. Membership testing, deleting duplicates from a sequence, and computing mathematical operations like intersection, union, difference, and symmetric difference are all done with sets.

Answered by lmiguelvargasf

Post is based on https://stackoverflow.com/questions/3489071/in-python-when-to-use-a-dictionary-list-or-set