Python Data Structures Cheat Sheet

In Python

Python is a popular programming language that is widely used in various fields such as data science, machine learning, web development, and more. One of the key features of Python is its support for various data structures that allow developers to store, manipulate, and organize data efficiently:

1. Lists: Lists are one of the most versatile data structures in Python. They are used to store a collection of items, such as numbers, strings, or other objects. Lists are mutable, which means that you can add, remove, or modify items in the list. To create a list, you can use square brackets [] and separate the items with commas.

2. Tuples: Tuples are similar to lists, but they are immutable, which means that you cannot modify them once they are created. Tuples are often used to store related data, such as a pair of coordinates or a date and time. To create a tuple, you can use parentheses () and separate the items with commas.

3. Sets: Sets are used to store a collection of unique items. They are mutable, and you can add or remove items from the set. Sets are often used to perform mathematical operations such as union, intersection, and difference. To create a set, you can use curly braces {} or the set() function.

4. Dictionaries: Dictionaries are used to store key-value pairs. They are mutable, and you can add, remove, or modify items in the dictionary. Dictionaries are often used to represent real-world objects, such as a person or a car. To create a dictionary, you can use curly braces {} and separate the key-value pairs with colons (:).

This cheat sheet provides an overview of the most commonly used data structures in Python. It includes a brief description of each data structure, its syntax, and some examples of how to use it.

Lists

Lists are one of the most commonly used data structures in Python. They are used to store a collection of items, which can be of different types.

SyntaxDescriptionExample
my_list = []Creates an empty listmy_list = []
my_list = [1, 2, 3]Creates a list with three elementsmy_list = [1, 2, 3]
my_list.append(item)Adds an item to the end of the listmy_list.append(4)
my_list.insert(index, item)Inserts an item at a specific indexmy_list.insert(0, 0)
my_list.remove(item)Removes the first occurrence of an item from the listmy_list.remove(1)
my_list.pop(index)Removes and returns the item at a specific indexmy_list.pop(0)
len(my_list)Returns the number of items in the listlen(my_list)
my_list[index]Returns the item at a specific indexmy_list[0]
my_list[start:end]Returns a slice of the list from start to endmy_list[1:3]

Tuples

Tuples are similar to lists, but they are immutable, which means that their contents cannot be changed once they are created.

SyntaxDescriptionExample
my_tuple = ()Creates an empty tuplemy_tuple = ()
my_tuple = (1, 2, 3)Creates a tuple with three elementsmy_tuple = (1, 2, 3)
len(my_tuple)Returns the number of items in the tuplelen(my_tuple)
my_tuple[index]Returns the item at a specific indexmy_tuple[0]
my_tuple[start:end]Returns a slice of the tuple from start to endmy_tuple[1:3]

Sets

Sets are used to store a collection of unique items. They are unordered and mutable.

SyntaxDescriptionExample
my_set = set()Creates an empty setmy_set = set()
my_set = {1, 2, 3}Creates a set with three elementsmy_set = {1, 2, 3}
my_set.add(item)Adds an item to the setmy_set.add(4)
my_set.remove(item)Removes an item from the setmy_set.remove(1)
len(my_set)Returns the number of items in the setlen(my_set)
item in my_setReturns True if the item is in the set, False otherwise2 in my_set

Dictionaries

Dictionaries are used to store a collection of key-value pairs. They are unordered and mutable.

SyntaxDescriptionExample
my_dict = {}Creates an empty dictionarymy_dict = {}
my_dict = {'key1': 'value1', 'key2': 'value2'}Creates a dictionary with two key-value pairsmy_dict = {'name': 'John', 'age': 30}
my_dict[key]Returns the value associated with the keymy_dict['name']
my_dict[key] = valueSets the value associated with the keymy_dict['name'] = 'Jane'
del my_dict[key]Removes the key-value pair from the dictionarydel my_dict['name']
len(my_dict)Returns the number of key-value pairs in the dictionarylen(my_dict)
key in my_dictReturns True if the key is in the dictionary, False otherwise'name' in my_dict

References