Python Data Structures Cheat Sheet
Python Data Structures Cheat Sheet
List
Package/
Description Code Example
Method
Syntax:
1. 1
1. list_name.append(element)
Copied!Wrap Toggled!
Example:
append() The `append()` method is used to add an element to the end of a list.
1. 1
2. 2
2. fruits.append("mango") print(fruits)
Copied!Wrap Toggled!
Example 1:
1. 1
2. 2
3. 3
copy() The `copy()` method is used to create a shallow copy of a list.
1. my_list = [1, 2, 3, 4, 5]
3. # Output: [1, 2, 3, 4, 5]
Copied!Wrap Toggled!
count() The `count()` method is used to count the number of occurrences of a specific element in a list in Python. Example:
1. 1
2. 2
3. 3
1. my_list = [1, 2, 2, 3, 4, 2, 5, 2]
3. # Output: 4
Copied!Wrap Toggled!
Example:
1. 1
A list is a built-in data type that represents an ordered and mutable collection of elements. Lists are
Creating a list 1. fruits = ["apple", "banana", "orange",
enclosed in square brackets [] and elements are separated by commas.
"mango"]
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
The `del` statement is used to remove an element from list. `del` statement removes the element at the
del 1. my_list = [10, 20, 30, 40, 50]
specified index.
2. del my_list[2] # Removes the element at
index 2 print(my_list)
extend() The `extend()` method is used to add multiple elements to a list. It takes an iterable (such as another list, Syntax:
tuple, or string) and appends each element of the iterable to the original list.
1. 1
1. list_name.extend(iterable)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
4. 4
3. fruits.extend(more_fruits)
4. print(fruits)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
Indexing in a list allows you to access individual elements by their position. In Python, indexing starts from
Indexing 1. my_list = [10, 20, 30, 40, 50]
0 for the first element and goes up to `length_of_list - 1`.
2. print(my_list[0])
4. print(my_list[-1])
1. 1
1. list_name.insert(index, element)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
1. my_list = [1, 2, 3, 4, 5]
2. my_list.insert(2, 6)
3. print(my_list)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
4. 4
Modifying a list You can use indexing to modify or assign new values to specific elements in the list. 1. my_list = [10, 20, 30, 40, 50]
element
3. print(my_list)
pop() `pop()` method is another way to remove an element from a list in Python. It removes and returns the Example 1:
element at the specified index. If you don't provide an index to the `pop()` method, it will remove and return
the last element of the list by default 1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
3. print(removed_element)
4. # Output: 30
5.
6. print(my_list)
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
3. print(removed_element)
4. # Output: 50
5.
6. print(my_list)
7. # Output: [10, 20, 30, 40]
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
4. 4
To remove an element from a list. The `remove()` method removes the first occurrence of the specified
remove() 1. my_list = [10, 20, 30, 40, 50]
value.
2. my_list.remove(30) # Removes the element
30
3. print(my_list)
Example 1:
1. 1
2. 2
3. 3
reverse() The `reverse()` method is used to reverse the order of elements in a list
1. my_list = [1, 2, 3, 4, 5]
2. my_list.reverse() print(my_list)
3. # Output: [5, 4, 3, 2, 1]
Copied!Wrap Toggled!
Slicing You can use slicing to access a range of elements from a list. Syntax:
1. 1
1. list_name[start:end:step]
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
1. my_list = [1, 2, 3, 4, 5]
2. print(my_list[1:4])
to 3)
4.
5. print(my_list[:3])
beginning up to index 2)
7.
8. print(my_list[2:])
to the end)
10.
11. print(my_list[::2])
element)
Copied!Wrap Toggled!
Example 1:
1. 1
2. 2
3. 3
4. 4
1. my_list = [5, 2, 8, 1, 9]
2. my_list.sort()
3. print(my_list)
4. # Output: [1, 2, 5, 8, 9]
Copied!Wrap Toggled!
The `sort()` method is used to sort the elements of a list in ascending order. If you want to sort the list in
sort() Example 2:
descending order, you can pass the `reverse=True` argument to the `sort()` method.
1. 1
2. 2
3. 3
4. 4
1. my_list = [5, 2, 8, 1, 9]
2. my_list.sort(reverse=True)
3. print(my_list)
4. # Output: [9, 8, 5, 2, 1]
Copied!Wrap Toggled!
Tuple
Package/ Description Code Example
Method
Syntax:
1. 1
1. tuple.count(value)
Copied!Wrap Toggled!
Example:
1. 1
The count() method for a tuple is used to count how many times a specified element appears
count() 2. 2
in the tuple.
3. 3
3. #Output: 2
Copied!Wrap Toggled!
index() The index() method in a tuple is used to find the first occurrence of a specified value and Syntax:
returns its position (index). If the value is not found, it raises a ValueError.
1. 1
1. tuple.index(value)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
3. #Output: 0
Copied!Wrap Toggled!
Syntax:
1. 1
1. sum(tuple)
Copied!Wrap Toggled!
Example:
The sum() function in Python can be used to calculate the sum of all elements in a tuple, 1. 1
sum()
provided that the elements are numeric (integers or floats).
2. 2
3. 3
2. print(sum(numbers))
3. #Output: 65
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
min() and max() Find the smallest (min()) or largest (max()) element in a tuple.
1. numbers = (10, 20, 5, 30)
2. print(min(numbers))
3. #Output: 5
4. print(max(numbers))
5. #Output: 30
Copied!Wrap Toggled!
len() Get the number of elements in the tuple using len(). Syntax:
1. 1
1. len(tuple)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
3. 3
3. #Output: 3