[Go to site: main page, start]

Data Structures

These articles are all about Python's core structures: lists, tuples, sets, and dictionaries.

24 articles · 1 hr 42 min read
18 screencasts · 1 hr 2 min watch
01
What are lists in Python?
3 min read Screencast available

Lists are used to store and manipulate an ordered collection of things.

Read
Read
02
How to make a tuple
5 min read Screencast available

Tuples are technically just immutable lists, but by convention we tend to use tuples and lists for quite different purposes.

Read
Read
03
List slicing
5 min read Screencast available

In Python, slicing looks like indexing with colons (:). You can slice a list (or any sequence) to get the first few items, the last few items, or all items in reverse.

Read
Read
04
Sequences
2 min read Screencast available

Sequences are iterables that have a length. Sequences are ordered collections (they maintain the order of their contents). The most common sequences built-in to Python are string, tuple, and list.

Read
Read
05
Checking for an empty list
2 min read Screencast available

Python programmers typically check for empty lists by relying on truthiness.

Read
Read
06
Python's list constructor: when and how to use it
6 min read

When should you use the built-in list function in Python? And when shouldn't you?

Read
Read
07
Using dictionaries
2 min read Screencast available

Python's dictionaries act as lookup tables which map keys to their values.

Read
Read
08
Avoid indexes
6 min read

Python code that uses indexes can often be refactored into more readable code that doesn't rely on indexing.

Read
Read
09
Flatten a list of lists
5 min read

Have a nested list-of-lists in Python? What's the most idiomatic way to flatten it?

Read
Read
10
Remove duplicates from a list
3 min read

There's more than one way deduplicate an iterable in Python. Which approach you take will depend on whether you care about the order of your items.

Read
Read
11
Counting occurrences in Python with collections.Counter
5 min read

Python's collections.Counter class is extremely handy, especially when paired with generator expressions.

Read
Read
12
Python Big O: the time complexities of different data structures
9 min read

The time complexity of common operations on Python's many data structures.

Read
Read
13
Merging dictionaries
5 min read Screencast available

Merging dictionaries in Python is usually as simple as a single pipe character.

Read
Read
14
Practical uses of sets
5 min read Screencast available

Sets are unordered collections of values that are great for removing duplicates, quick containment checks, and set operations.

Read
Read
15
List containment checks
3 min read Screencast available Premium

The in operator on lists can be slow. But why?

Read
Read
16
Looping over dictionaries
3 min read Screencast available Premium

What happens when you loop over a dictionary? And should you loop over dictionaries?

Read
Read
17
What is a mapping?
3 min read Screencast available Premium

A mapping is a dictionary-like object. Some mappings are mutable and some are immutable.

Read
Read
18
Removing a dictionary key
4 min read Screencast available Premium

There is more than one way to remove a key-value pair from a dictionary in Python.

Read
Read
19
Are dictionaries ordered in Python?
3 min read Screencast available Premium

Dictionaries in Python maintain the insertion order of their keys.

Read
Read
20
The list insert method
5 min read Screencast available Premium

Inserting an item to the beginning of a list is much slower than appending an item to the end of a list.

Read
Read
21
Removing list items
3 min read Screencast available Premium

List items can be removed based on their value or position in the list. Removing from the end of a list using is the most common and efficient approach.

Read
Read
22
Equality with data structures
3 min read Screencast available Premium

Data structures can be compared for equality in Python by using an equality check with the == operator.

Read
Read
23
Lexicographical ordering
5 min read Screencast available Premium

Python lexicographically orders tuples, strings, and all other sequences, comparing element-by-element.

Read
Read
24
Setting default dictionary values
7 min read Screencast available

There are many ways to set default values for dictionary key lookups in Python. Which way you should use will depend on your use case.

Read
Read
Continue exploring
Profile picture of Trey
Learn something new about Python every week

My name is Trey Hunner. I publish new Python articles and screencasts every week through Python Morsels. If you want to learn something new about Python every week, join Python Morsels!

Join Python Morsels ✨