Lists
Lists
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
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)
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]
Example 2:
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:
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
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
about:blank 3/3