0% found this document useful (0 votes)
7 views

Python Data Structures Cheat Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Data Structures Cheat Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

10/12/2024, 12:14 about:blank

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

A list is a built-in data type that represents an Example:


ordered and mutable collection of elements.
Creating a list
Lists are enclosed in square brackets [] and fruits = ["apple", "banana", "orange", "mango"]
elements are separated by commas.

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)

The `insert()` method is used to insert an Example:


insert()
element.
my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 6)
print(my_list)

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]

pop() `pop()` method is another way to remove an Example 1:


element from a list in Python. It removes and
my_list = [10, 20, 30, 40, 50]
returns the element at the specified index. If removed_element = my_list.pop(2) # Removes and returns the element at index 2
you don't provide an index to the `pop()` print(removed_element)
method, it will remove and return the last # Output: 30
element of the list by default print(my_list)
# Output: [10, 20, 40, 50]

Example 2:

my_list = [10, 20, 30, 40, 50]


removed_element = my_list.pop() # Removes and returns the last element
print(removed_element)

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:

The index() method in a tuple is used to tuple.index(value)


find the first occurrence of a specified
index() value and returns its position (index). If Example:
the value is not found, it raises a fruits = ("apple", "banana", "orange")
ValueError. print(fruits[1]) #Returns the value at which apple is present.
#Output: banana

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

len() Get the number of elements in the tuple Syntax:


using len().
len(tuple)

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

© IBM Corporation. All rights reserved.

about:blank 3/3

You might also like