Python Lists and Dictionaries Explained

Content:

Python includes several structures for storing data. Lists and dictionaries are probably the two most frequently used. They are both functionally similar to arrays, which are common in many programming languages.

There are, however, a few important differences between lists and dictionaries.

In this article, we’ll go over the two data structures, highlighting the differences, and explaining when each structure should be used.

Lists

Lists in Python are flexible data structures, which are able to store multiple values in a single variable.

Unlike dictionaries, lists do not have keys, instead referring to values by indexes. They maintain the order of the elements that are entered, while also providing sorting functionality to allow sorting by value.

To access a value in a list, use the [] notation with the index of the item to be returned.

fruits = ['apple', 'orange', 'pear']
print(fruits[0])    # apple

If you’re looking to store single values, lists are a good option to do so.

Content

Creating a new empty list can be done using []. To initialise a list with values, add each value between the [], separated by commas.

empty = []
fruits = ['apple', 'orange', 'pear']

Adding items to an existing list can be done using append().

fruits = ['apple', 'orange', 'pear']
fruits.append('strawberry')
print(fruits)    # ['apple', 'orange', 'pear', 'strawberry']

Altering an existing element can be done by setting the list element with the relevant index.

fruits = ['apple', 'orange', 'pear']
fruits[1] = 'strawberry'
print(fruits)    # ['apple', 'strawberry', 'pear']

There are a few ways an element can be removed. To remove an element by value, use remove().

fruits = ['apple', 'orange', 'pear']
fruits.remove('apple')
print(fruits)    # ['orange', 'pear']

To remove by index, use pop(). You can also use del to achieve the same thing.

fruits = ['apple', 'orange', 'pear']
fruits.pop(0)
del fruits[0]
print(fruits)    # ['orange', 'pear']
del fruits[0]
print (fruits)   # ['pear']

If called with no index specified, the last element will be removed.

fruits = ['apple', 'orange', 'pear']
fruits.pop()
print(fruits)    # ['apple', 'orange']

To empty the entire list, use clear().

fruits = ['apple', 'orange', 'pear']
fruits.clear()
print(fruits)    # []

Iterating

Iterating a list can be done using a for loop.

fruits = ['apple', 'orange', 'pear']
for fruit in fruits:
    print(fruit)    # apple (1st iteration)

Ordering

Lists maintain the order of the items added, unless they are sorted using sort(). This makes it possible to either keep track of item order, or easily sort the contents of the list.

Each list entry has an integer index, defining its position within the list. As is common in programming, list indices start at 0.

fruits = ['apple', 'orange', 'pear']
print(fruits[2])    # pear

Reversing the ordering of the list can be done using reverse().

fruits = ['apple', 'orange', 'pear']
fruits = fruits.reverse()
print(fruits[2])    # apple

Sorting

Basic sorting can be done using sort().

fruits = ['pear', 'apple', 'orange']
fruits = fruits.sort()
print(fruits)    # ['apple', 'orange', 'pear']

In addition, a more powerful sorting function is available, named sorted(). sorted() takes an optional reverse parameter, which allows sorting in reverse order.

fruits = ['orange', 'strawberry', 'apple', 'pear']
print(sorted(fruits, reverse=True))    # ['strawberry', 'pear', 'orange', 'apple']

When sorting strings, uppercase characters are sorted first due to having character codes with a lower ID value.

fruits = ['orange', 'Orange']
print(sorted(fruits))    # ['Orange', 'orange']

A second optional parameter, key, can be used to apply a function to each element before sorting.

fruits = ['orange', 'Strawberry', 'apple', 'pear']
print(sorted(fruits, key=str.lower))    # ['apple', 'orange', 'pear', 'Strawberry']

Note that ‘Strawberry’ is sorted last here, despite starting with an uppercase character. This is due to the str.lower key transforming the value for comparison.

It’s possible to pass the name of a user-defined function to key, to add custom transformations during comparison.

Dictionaries

Dictionaries, shortened to dict when using type hints, store a collection of key-value pairs. Unlike lists, the values held in a dictionary can be of any data type. Each key can only exist once.

To access the values in a dictionary, you can either use [], or the get() function.

clothing = {
    "shirt": 10.00,
    "trainers": 30.00,
    "shorts": 7.00,
    "sunglasses": 15.00
}
print(clothing['shirt'])         # 10.00
print(clothing.get('shorts'))    # 7.00

The difference between the two lies in the handling of a missing key. While [] will through a KeyError exception, get() will return a default value. This can be specified as the second parameter, or will return None if left empty.

print(clothing['jeans'])                 # KeyError exception
print(clothing.get('jeans')              # None
print(clothing.get('jeans', 'shorts')    # 7.00

Content

New empty dictionaries can be created using {}. To create a new dictionary including content, key and value should be separated by a :. While they differ internally in their representation, the syntax used for dictionaries is the same as the syntax used for JSON.

empty = {}
clothing = {
    "shirt": 10.00,
    "trainers": 30.00,
    "shorts": 7.00,
    "sunglasses": 15.00
}

Strings should be enclosed in either ' or ".

Items can be added to a dictionary using [].

clothing['jeans'] = 15.00

Using an existing key name will change the value for that key.

Removing an item can be done using pop(), similar to a list.

clothing.pop('shorts')

The clear() function will empty all items from the dictionary. Again, this is similar to the list element.

clothing.clear()

Iterating

With dictionaries having both keys and values, there are a few ways to iterate through them.

The keys() function returns an object containing the keys.

print(clothing.keys())    # ["shirt", "trainers"...]

This object acts as a dynamic view to the dictionary, and automatically updates as the contents of the dictionary are changed.

The result can be iterated through to obtain each individual key.

for key in clothing.keys():
    print(key)    # "shirt"

The items() function returns an object of tuples, which includes both the key and the value. This also acts as a dynamic view, which updates as the dictionary contents change.

print(clothing.items())    # [("shirt", 10.00), ("trainers", 30.00)...]

It’s possible to loop through this result, to access each individual tuple.

for item in clothing.items():
    print(item)    # ("shirt", 10.00)

It’s also possible to unpack the tuple directly into variables.

for key, value in clothing.items():
    print(key)      # "shirt"
    print(value)    # 10.00

This is a common method of looping through the items in a dictionary.

Ordering

Prior to Python 3.7, dictionaries were unordered. This meant that the contents of the dictionary had no defined position within the dictionary. Since Python 3.7, dictionaries are now ordered, and have a fixed position in the dictionary.

Sorting

The ordering change in Python 3.7 made sorting dictionary elements much easier. Sorting can be done using the sorted() function, in combination with items().

sorted_clothing = {sorted(clothing.items())}
print(sorted_clothing)    
# clothing = {
#     "shirt": 10.00,
#     "shorts": 7.00,
#     "sunglasses": 15.00,
#     "trainers": 30.00
# }

Prior to Python 3.7, sorting was much more difficult, due to dictionaries not retaining their order. It is advisable to use another data structure for pre-3.7 sorting where possible.

Conclusion

There are many similarities between the two data structures, but a few key differences which dictate where each is suitable to use.

If you need to store key-value pairs, dictionaries are the structure to use. In addition, searching for the existence of a key is much faster than checking for a list value. With large data sets, a dictionary could be more appropriate even if key-value pairs are not strictly necessary due to this faster searching.

Otherwise, a list is likely to be the better choice.