Skip to content

#208: Working With Lists and Dictionaries

I had to do a lot of work with lists and dictionaries to solve the puzzles for Advent of Code. Here is my little cheat sheet for these two basic data structures from which you could also benefit.

Length of a list

1
2
3
>>> data = [1,2,3,4,5]
>>> len(data)
5

Copy a list

1
2
3
4
5
6
7
>>> data = [1,2,3,4,5]
>>> more = data.copy()
>>> data.append(6)
>>> data
[1, 2, 3, 4, 5, 6]
>>> more
[1, 2, 3, 4, 5]

Append at the end of a list

1
2
3
4
>>> data = [1,2,3,4,5]
>>> data.append(6)
>>> data
[1, 2, 3, 4, 5, 6]

Append at the beginning of a list

1
2
3
4
>>> data = [1,2,3,4,5]
>>> data.insert(0, 6)
>>> data
[6, 1, 2, 3, 4, 5]

Reverse a list (in place)

1
2
3
4
>>> data = [1,2,3,4,5]
>>> data.reverse()
>>> data
[5, 4, 3, 2, 1]

Create a reversed copy of a list

>>> data = [1,2,3,4,5]
>>> list(reversed(data))
[5, 4, 3, 2, 1]
>>> data
[1, 2, 3, 4, 5]

>>> data = [1,2,3,4,5]
>>> back = data[::-1]
>>> back
[5, 4, 3, 2, 1]
>>> data
[1, 2, 3, 4, 5]

Sum up all entries in a list

1
2
3
>>> data = [1,2,3,4,5]
>>> sum(data)
15

Multiply all entries in a list

1
2
3
4
>>> import numpy as np
>>> data = [1,2,3,4,5]
>>> np.prod(data)
120

Find the least common multiple

1
2
3
4
>>> import math
>>> data = [1,2,3,4,5]
>>> math.lcm(*data)
60

Get maximum and minimum of entries in a list

1
2
3
4
5
>>> data = [1,2,3,4,5]
>>> min(data)
1
>>> max(data)
5

Sort a list (in place)

1
2
3
4
5
6
7
>>> data = [2,4,1,5,3]
>>> data.sort()
>>> data
[1, 2, 3, 4, 5]
>>> data.sort(reverse=True)
>>> data
[5, 4, 3, 2, 1]

Sort a copy of the list

1
2
3
4
5
6
7
8
9
>>> data = [2,4,1,5,3]
>>> sorted(data)
[1, 2, 3, 4, 5]
>>> data
[2, 4, 1, 5, 3]
>>> sorted(data, reverse=True)
[5, 4, 3, 2, 1]
>>> data
[2, 4, 1, 5, 3]

Sort a list by a property of the entries

>>> from typing import NamedTuple
>>> class Dto(NamedTuple):
...     name: str
...     value: int
...
>>> a = Dto("a", 2)
>>> b = Dto("b", 1)
>>> c = Dto("c", 7)
>>> d = Dto("d", 3)
>>> data = [a, d, c, b]
>>> sorted(data, key=lambda x: x.value)
[Dto(name='b', value=1), Dto(name='a', value=2), Dto(name='d', value=3), Dto(name='c', value=7)]
>>> sorted(data, key=lambda x: x.value, reverse=True)
[Dto(name='c', value=7), Dto(name='d', value=3), Dto(name='a', value=2), Dto(name='b', value=1)]
>>> sorted(data, key=lambda x: x.name)
[Dto(name='a', value=2), Dto(name='b', value=1), Dto(name='c', value=7), Dto(name='d', value=3)]
>>> sorted(data, key=lambda x: x.name, reverse=True)
[Dto(name='d', value=3), Dto(name='c', value=7), Dto(name='b', value=1), Dto(name='a', value=2)]

Turn a string into a list of characters

1
2
3
>>> hi = "Hello"
>>> list(hi)
['H', 'e', 'l', 'l', 'o']

Turn a list into a string

1
2
3
4
5
6
>>> data = ['H', 'e', 'l', 'l', 'o']
>>> "".join(data)
'Hello'
>>> data = ['H', 'e', 'l', 'l', 'o', 1, 2, 3]
>>> "".join(str(x) for x in data)
'Hello123'

Iterate through a list

>>> data = ['H', 'e', 'l', 'l', 'o', 1, 2, 3]
>>> for value in data:
...     print(value)
...
H
e
l
l
o
1
2
3

Iterate through a list with an index

>>> data = ['H', 'e', 'l', 'l', 'o', 1, 2, 3]
>>> for index, value in enumerate(data):
...     print(f"{index}. => {value}")
...
0. => H
1. => e
2. => l
3. => l
4. => o
5. => 1
6. => 2
7. => 3

Get the largest value of a dictionary

1
2
3
>>> data = {"a":4, "b":2, "d":3, "e":5, "f":1}
>>> max(data.values())
5

Get the largest key of a dictionary

1
2
3
>>> data = {"a":4, "b":2, "d":3, "e":5, "f":1}
>>> max(data.keys())
'f'

Get the key for the largest value in a dictionary

1
2
3
>>> data = {"a":4, "b":2, "d":3, "e":5, "f":1}
>>> max(data, key=data.get)
'e'

Next

This cheat sheet may be of help for your next attempt with Advent of Code of when you need to do some work with lists and dictionaries. Next week we explore defaultdict, one more thing about dictionaries and lists that can make your code simpler.