Cheat Sheet Tuplas y Listas
Cheat Sheet Tuplas y Listas
List
Package/
Description Code Example
Method
Syntax:
1. list_name.app
end(element)
Example:
The `append()` method is used to add 1. fruits =
append()
an element to the end of a list. ["apple",
"banana",
"orange"]
2. fruits.append(
"mango")
print(fruits)
Example 1:
1. my_list = [1,
2, 3, 4, 5]
The `copy()` method is used to create a
copy() 2. new_list =
shallow copy of a list.
my_list.copy()
print(new_list)
3. # Output: [1,
2, 3, 4, 5]
Example:
1. my_list = [1,
The `count()` method is used to count 2, 2, 3, 4, 2, 5,
count() the number of occurrences of a specific 2]
element in a list in Python. 2. count =
my_list.count(
2) print(count)
3. # Output: 4
Example:
A list is a built-in data type that
represents an ordered and mutable 1. fruits =
Creating a
collection of elements. Lists are ["apple",
list
enclosed in square brackets [] and "banana",
elements are separated by commas. "orange",
"mango"]
Syntax:
1. list_name.exte
nd(iterable)
Example:
The `extend()` method is used to add 1. fruits =
multiple elements to a list. It takes an ["apple",
extend() iterable (such as another list, tuple, or "banana",
string) and appends each element of "orange"]
the iterable to the original list. 2. more_fruits =
["mango",
"grape"]
3. fruits.extend(
more_fruits)
4. print(fruits)
Example:
1. my_list = [10,
20, 30, 40, 50]
2. print(my_list[0
])
Indexing in a list allows you to access
3. # Output: 10
individual elements by their position. In
(accessing the
Indexing Python, indexing starts from 0 for the
first element)
first element and goes up to
4. print(my_list[-
`length_of_list - 1`.
1])
5. # Output: 50
(accessing the
last element
using negative
indexing)
Syntax:
1. list_name.inse
rt(index,
element)
The `insert()` method is used to insert Example:
insert()
an element.
1. my_list = [1,
2, 3, 4, 5]
2. my_list.insert(
2, 6)
3. print(my_list)
Example:
1. my_list = [10,
20, 30, 40, 50]
2. my_list[1] =
You can use indexing to modify or
Modifying a 25 #
assign new values to specific elements
list Modifying the
in the list.
second
element
3. print(my_list)
4. # Output: [10,
25, 30, 40, 50]
Example 1:
1. my_list = [10,
20, 30, 40, 50]
2. removed_elem
ent =
my_list.pop(2)
# Removes
and returns
the element at
index 2
3. print(removed
_element)
4. # Output: 30
`pop()` method is another way to 5.
remove an element from a list in 6. print(my_list)
Python. It removes and returns the 7. # Output: [10,
pop() element at the specified index. If you 20, 40, 50]
don't provide an index to the `pop()` Example 2:
method, it will remove and return the 1. my_list = [10,
last element of the list by default 20, 30, 40, 50]
2. removed_elem
ent =
my_list.pop()
# Removes
and returns
the last
element
3. print(removed
_element)
4. # Output: 50
5.
6. print(my_list)
7. # Output: [10,
20, 30, 40]
Example 1:
1. my_list = [1,
2, 3, 4, 5]
The `reverse()` method is used to
reverse() 2. my_list.revers
reverse the order of elements in a list
e()
print(my_list)
3. # Output: [5,
4, 3, 2, 1]
Example 1:
1. my_list = [5,
2, 8, 1, 9]
2. my_list.sort()
3. print(my_list)
The `sort()` method is used to sort the 4. # Output: [1,
elements of a list in ascending order. If 2, 5, 8, 9]
sort() you want to sort the list in descending Example 2:
order, you can pass the `reverse=True`
argument to the `sort()` method. 1. my_list = [5,
2, 8, 1, 9]
2. my_list.sort(re
verse=True)
3. print(my_list)
4. # Output: [9,
8, 5, 2, 1]
Tuple
Package/
Description Code Example
Method
Syntax:
1. tuple.count(value)
Example:
Syntax:
Example:
1. numbers = (10,
20, 5, 30)
min() and Find the smallest (min()) or largest 2. print(min(numbers
max() (max()) element in a tuple. ))
3. #Output: 5
4. print(max(number
s))
5. #Output: 30
Syntax:
1. len(tuple)
Example: