Python data structures cheat sheet

Eve 145 Published: 06/16/2024

Python data structures cheat sheet

Here is a comprehensive Python data structures cheat sheet:

Lists

A list is a mutable sequence of items. Lists are denoted by square brackets []. You can create an empty list using the syntax []. You can create a list with initial values using the syntax [1, 2, 3]. Indexing starts at 0, so the first element is at index 0, and the last element is at index -1 (when the length of the list is more than one). You can access elements in a list using square brackets [] with an index. You can change individual elements by assigning a new value to an existing index. You can append or insert new elements into a list using methods such as append(), extend(), insert(), and remove(). Lists support concatenation, slicing, sorting, and searching.

Tuples

A tuple is an immutable sequence of items. Tuples are denoted by parentheses ( ) or square brackets []. You can create a tuple using the syntax (1, 2, 3). Indexing starts at 0, so the first element is at index 0, and the last element is at index -1 (when the length of the tuple is more than one). Tuples are immutable, meaning you cannot change their contents. You can use tuples for tasks that require immutability, such as storing data in a database or sending data over a network.

Sets

A set is an unordered collection of unique elements. Sets are denoted by curly braces { }. You can create a set using the syntax {1, 2, 3}. Sets automatically eliminate duplicate values. Indexing does not apply to sets because they are unordered collections. Sets support union, intersection, and difference operations.

Dictionaries

A dictionary is an unordered collection of key-value pairs. Dictionaries are denoted by curly braces { }. You can create a dictionary using the syntax {'name': 'John', 'age': 30}. Keys must be immutable (e.g., strings or tuples). Values can be any type, including other dictionaries. Indexing starts at 0 for values, but not for keys. Dictionaries support concatenation, slicing, and searching by key.

Stacks

A stack is a last-in-first-out (LIFO) data structure. You can implement a stack using lists or classes with methods for pushing and popping elements. The append() method pushes an element onto the top of the stack. The pop() method removes the top element from the stack.

Queues

A queue is a first-in-first-out (FIFO) data structure. You can implement a queue using lists or classes with methods for enqueueing and dequeueing elements. The append() method adds an element to the end of the queue. The pop(0) method removes the front element from the queue.

Trees

A tree is a hierarchical data structure that consists of nodes and edges. Nodes have children, but not necessarily parents. You can implement a tree using classes with methods for creating new nodes, adding children, and traversing the tree. Trees support operations such as searching, inserting, deleting, and sorting.

Graphs

A graph is an unordered collection of vertices connected by edges. You can represent a graph using lists or dictionaries to store vertices and edges. Graphs support operations such as finding shortest paths, detecting cycles, and performing topological sorts.

This cheat sheet should provide you with a comprehensive overview of the most common Python data structures.

What are the Python data structures?

Here's a detailed overview of Python data structures:

1. Lists

Lists are ordered collections of items that can be of any data type, including strings, integers, floats, and other lists. They are denoted by square brackets [] and elements are separated by commas. Lists are mutable, meaning they can be modified after creation.

Example: my_list = [1, 2, 3, "hello", 4]

2. Tuples

Tuples are similar to lists but are immutable, meaning they cannot be changed after creation. They are denoted by parentheses () and elements are separated by commas.

Example: my_tuple = (1, 2, 3, "hello")

3. Dictionaries

Dictionaries are unordered collections of key-value pairs. Keys can be strings or integers, while values can be any data type. They are denoted by curly braces {} and elements are separated by commas.

Example: my_dict = {"name": "John", "age": 30}

4. Sets

Sets are unordered collections of unique items. They can contain any data type, including strings, integers, and other sets. Sets are mutable and support various set operations like union, intersection, and difference.

Example: my_set = {1, 2, 3, "hello", "world"}

5. Strings

Strings are sequences of characters, such as letters, digits, or symbols. They are denoted by quotes "'" or apostrophes '. Strings are immutable in Python, but they support various operations like concatenation, slicing, and formatting.

Example: my_string = "Hello, world!"

6. Arrays

Arrays are similar to lists, but they can only contain elements of the same data type. In Python, arrays are not built-in, but you can create them using libraries like NumPy or PyArray.

7. Bytearrays

Bytearrays are sequences of bytes that can be used to store binary data. They are denoted by b prefix and support various operations like slicing and formatting.

Example: my_bytearray = b"Hello, world!"

In summary, Python's built-in data structures include lists, tuples, dictionaries, sets, strings, and bytearrays. Understanding these data structures is crucial for efficient coding in Python and working with various types of data.