Prev: Combining dictionary and list | Next: Dictionary functions & methods
Similarities
- Both are indexed: list is integer-based index while dict is key-based indexed (key should be of immutable type)
- Both are mutable: changing their items can happen "in place" (in memory they occupy without constructing new object)
- Both are iterable: they possess a __next__ method that returns items one by one (but remember that for a dict this happens without any particular order)
Differences
- List is a sequence - Dict is a collection: however, len() function operates properly for both of them returning their lenght (number of items they contain; in a dictionary an item is a key:value pair)
- List is ordered - Dict is not: a list is an ordered object (can be sorted). A dict is unordered, ordering is meaningless and items may returned in any order (careful when you are using a dict as a iterable)
- List uses integer-based indexing - Dict can use any immutable type as index: this makes dict based code much more readable and understandable by humans; for example, you get more information about what the code does if you read di['client_name'] compared to li[1]
- Dict is mapping - List is not: a dict always constructs some type of mapping between keys and values. A list is always some sequence of single items.
Use a list:
- ...when you need a flexible sequence for all kind of computations and when integer-based indexing does not impose significant restrictions on your data representation
- ...also when ordering (sorting, etc.) of data is important
Use a dict:
- ...when you need a 'key:value' representation of data, which essentialy is a "look-up" table: you look for a value using a key
- ...also if you have to construct a sparse collection of items than a dense one
For example:
import random
alist = [random.randint(1,100) for i in range(10)]
alist.sort()
alist
movies = {1975: 'Holy Grail',
1979: 'Life of Brian',
1983: 'The Meaning of Life'}
print(movies,'\n') # movies are printed without any specific order
sorted_years = sorted(movies.keys()) # sorted_years is now a sorted list of keys
print(sorted_years,'\n')
for year in sorted_year:
print(year, movies[year])
# A simple OrderedDict example
import collections
country = [('Name','France'),('Capital city','Paris'),('Population',50),('Currency','Euro')]
dunord = dict(country)
dord = collections.OrderedDict(country)
for i in dunord:
print(i, end=' ')
print('\n')
for i in dord:
print(i, end=' ')
- ...although list is a very flexible structure this flexibility requires that several particular list-relevant processing is happenning 'under the hood'. Thus, the price for flexibility is reduced efficiency.
- Reduced efficiency is not a problem when data size is relatively small. But, it really turns to be a restriction when dealing with big data. This is also the reason for introducing a more efficient object, namely 'ndarray' (learn more about ndarrays in numpy section).
Dict is efficient because...:
- ...a dict is constructed as a hash table. In general, a hash table uses a hash function to implement a mapping between input keys and output values. The advantage is that the time required to perform a task in a dict (for example, access a certain item) is constant as compared to the time required in a list which increases as the size of the list increases.
- Read more on hash table here
- Technically, the above difference is reported using the big-O notation by stating that dict performance is of O(1) (meaning: standard) while list performance is of O(n) (increases as n list size increases)
You can read more on this issue and also see a graphical representation of dict-list efficiency here
- ~ 18.5 msec for the list
- ~ 95.5 nsec for the dict (with 1 msec = 1000 nsec)
# timeit example
alist = [i for i in range(1000000)]
adict = {alist[i] : alist[i] for i in alist}
k = 999999
%timeit k in alist
%timeit k in adict
. Free learning material
. See full copyright and disclaimer notice