0% found this document useful (0 votes)
2 views3 pages

Lists

This document is a cheat sheet for Python data structures, specifically focusing on lists and tuples. It provides descriptions and code examples for various methods and functionalities such as append, copy, count, and sort for lists, as well as count, index, and sum for tuples. The cheat sheet serves as a quick reference for using these data structures effectively in Python.

Uploaded by

neha khairwa
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)
2 views3 pages

Lists

This document is a cheat sheet for Python data structures, specifically focusing on lists and tuples. It provides descriptions and code examples for various methods and functionalities such as append, copy, count, and sort for lists, as well as count, index, and sum for tuples. The cheat sheet serves as a quick reference for using these data structures effectively in Python.

Uploaded by

neha khairwa
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

1/27/25, 9:06 PM 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
append() Example:
an 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 my_list = [1, 2, 3, 4, 5]
copy()
a 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
count() the number of occurrences of a my_list = [1, 2, 2, 3, 4, 2, 5, 2]
count = my_list.count(2) print(count)
specific element in a list in Python. # Output: 4

A list is a built-in data type that


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

The `del` statement is used to remove Example:


an element from list. `del` statement my_list = [10, 20, 30, 40, 50]
del
removes the element at the specified del my_list[2] # Removes the element at index 2 print(my_list)
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 Example:
extend() iterable (such as another list, tuple, or
string) and appends each element of fruits = ["apple", "banana", "orange"]
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
individual elements by their position. my_list = [10, 20, 30, 40, 50]
Indexing In Python, indexing starts from 0 for print(my_list[0])
# Output: 10 (accessing the first element)
the first element and goes up to print(my_list[-1])
`length_of_list - 1`. # Output: 50 (accessing the last element using negative indexing)

Syntax:
list_name.insert(index, element)

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


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

Example:
You can use indexing to modify or my_list = [10, 20, 30, 40, 50]
Modifying a list assign new values to specific my_list[1] = 25 # Modifying the second element
elements in the list. print(my_list)
# Output: [10, 25, 30, 40, 50]

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


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

Example 2:

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

about:blank 1/3
1/27/25, 9:06 PM about:blank
removed_element = my_list.pop() # Removes and returns the last element
print(removed_element)
# 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 my_list = [1, 2, 3, 4, 5]
reverse() my_list.reverse() print(my_list)
reverse the order of elements in a 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 print(my_list[1:4])
Slicing
of 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. # Output: [1, 2, 5, 8, 9]
If you want to sort the list in
sort()
descending order, you can pass the Example 2:
`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 how many times a Example:
count()
specified element 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 tuple.index(value)
used to find the first occurrence of
index() a specified value and returns its Example:
position (index). If the value is fruits = ("apple", "banana", "orange")
not found, it raises a ValueError. print(fruits[1]) #Returns the value at which apple is present.
#Output: banana

Syntax:

The sum() function in Python can sum(tuple)


be used to calculate the sum of all
sum() elements in a tuple, provided that Example:
the elements are numeric (integers
numbers = (10, 20, 5, 30)
or floats). print(sum(numbers))
#Output: 65

Example:
numbers = (10, 20, 5, 30)
Find the smallest (min()) or print(min(numbers))
min() and max() #Output: 5
largest (max()) element in a tuple.
print(max(numbers))
#Output: 30

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


tuple using len().
len(tuple)

about:blank 2/3
1/27/25, 9:06 PM 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