Python Data Structures Cheat Sheet
Python Data Structures Cheat Sheet
List
Package/Method Description Code Example
Syntax:
list_name.append(element)
The `append()` method is used to add an
append() Example:
element to the end of a list.
fruits = ["apple", "banana", "orange"]
fruits.append("mango") print(fruits)
Example 1:
The `copy()` method is used to create a my_list = [1, 2, 3, 4, 5]
copy()
shallow copy of a list. new_list = my_list.copy() print(new_list)
# Output: [1, 2, 3, 4, 5]
Example:
The `count()` method is used to count the
count() number of occurrences of a specific element my_list = [1, 2, 2, 3, 4, 2, 5, 2]
count = my_list.count(2) print(count)
in a list in Python. # Output: 4
Example:
The `del` statement is used to remove an
del element from list. `del` statement removes the my_list = [10, 20, 30, 40, 50]
del my_list[2] # Removes the element at index 2 print(my_list)
element at the specified index. # Output: [10, 20, 40, 50]
Syntax:
list_name.extend(iterable)
The `extend()` method is used to add multiple
elements to a list. It takes an iterable (such as Example:
extend()
another list, tuple, or string) and appends each
fruits = ["apple", "banana", "orange"]
element of the iterable to the original list. more_fruits = ["mango", "grape"]
fruits.extend(more_fruits)
print(fruits)
Example:
Indexing in a list allows you to access my_list = [10, 20, 30, 40, 50]
individual elements by their position. In print(my_list[0])
Indexing
Python, indexing starts from 0 for the first # Output: 10 (accessing the first element)
element and goes up to `length_of_list - 1`. print(my_list[-1])
# Output: 50 (accessing the last element using negative indexing)
Syntax:
list_name.insert(index, element)
Example:
You can use indexing to modify or assign new my_list = [10, 20, 30, 40, 50]
Modifying a list my_list[1] = 25 # Modifying the second element
values to specific elements in the list. print(my_list)
# Output: [10, 25, 30, 40, 50]
Example 2:
about:blank 1/3
10/12/2024, 12:14 about:blank
# Output: 50
print(my_list)
# Output: [10, 20, 30, 40]
Example:
To remove an element from a list. The my_list = [10, 20, 30, 40, 50]
remove() `remove()` method removes the first my_list.remove(30) # Removes the element 30
occurrence of the specified value. print(my_list)
# Output: [10, 20, 40, 50]
Example 1:
The `reverse()` method is used to reverse the my_list = [1, 2, 3, 4, 5]
reverse()
order of elements in a list my_list.reverse() print(my_list)
# Output: [5, 4, 3, 2, 1]
Syntax:
list_name[start:end:step]
Example:
my_list = [1, 2, 3, 4, 5]
You can use slicing to access a range of print(my_list[1:4])
Slicing
elements from a list. # Output: [2, 3, 4] (elements from index 1 to 3)
print(my_list[:3])
# Output: [1, 2, 3] (elements from the beginning up to index 2)
print(my_list[2:])
# Output: [3, 4, 5] (elements from index 2 to the end)
print(my_list[::2])
# Output: [1, 3, 5] (every second element)
Example 1:
my_list = [5, 2, 8, 1, 9]
my_list.sort()
The `sort()` method is used to sort the print(my_list)
elements of a list in ascending order. If you # Output: [1, 2, 5, 8, 9]
sort() want to sort the list in descending order, you
Example 2:
can pass the `reverse=True` argument to the
`sort()` method. my_list = [5, 2, 8, 1, 9]
my_list.sort(reverse=True)
print(my_list)
# Output: [9, 8, 5, 2, 1]
Tuple
Package/Method Description Code Example
Syntax:
tuple.count(value)
The count() method for a tuple is used to
count() count how many times a specified element Example:
appears in the tuple. fruits = ("apple", "banana", "apple", "orange")
print(fruits.count("apple")) #Counts the number of times apple is found in tuple.
#Output: 2
Syntax:
Syntax:
sum(tuple)
The sum() function in Python can be used
to calculate the sum of all elements in a Example:
sum()
tuple, provided that the elements are
numeric (integers or floats). numbers = (10, 20, 5, 30)
print(sum(numbers))
#Output: 65
Example:
numbers = (10, 20, 5, 30)
Find the smallest (min()) or largest (max()) print(min(numbers))
min() and max()
element in a tuple. #Output: 5
print(max(numbers))
#Output: 30
about:blank 2/3
10/12/2024, 12:14 about:blank
Example:
fruits = ("apple", "banana", "orange")
print(len(fruits)) #Returns length of the tuple.
#Output: 3
about:blank 3/3