0% found this document useful (0 votes)
24 views6 pages

Cheat Sheet Tuplas y Listas

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 associated with these data types, such as `append()`, `copy()`, `count()`, and others for lists, as well as `count()`, `index()`, and `sum()` for tuples. The document serves as a quick reference for understanding and using these data structures in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Cheat Sheet Tuplas y Listas

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 associated with these data types, such as `append()`, `copy()`, `count()`, and others for lists, as well as `count()`, `index()`, and `sum()` for tuples. The document serves as a quick reference for understanding and using these data structures in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Data Structures Cheat Sheet

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"]

del The `del` statement is used to remove Example:


an element from list. `del` statement
1. my_list = [10,
removes the element at the specified
20, 30, 40, 50]
index.
2. del my_list[2]
# Removes
the element at
index 2
print(my_list)
3. # Output: [10,
20, 40, 50]

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]

remove() To remove an element from a list. The Example:


`remove()` method removes the first
1. my_list = [10,
occurrence of the specified value.
20, 30, 40, 50]
2. my_list.remov
e(30) #
Removes the
element 30
3. print(my_list)
4. # Output: [10,
20, 40, 50]

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]

Slicing You can use slicing to access a range of Syntax:


elements from a list.
1. list_name[star
t:end:step]
Example:
1. my_list = [1,
2, 3, 4, 5]
2. print(my_list[1
:4])
3. # Output: [2,
3, 4]
(elements
from index 1
to 3)
4.
5. print(my_list[:
3])
6. # Output: [1,
2, 3]
(elements
from the
beginning up
to index 2)
7.
8. print(my_list[2
:])
9. # Output: [3,
4, 5]
(elements
from index 2
to the end)
10.
11.print(my_list[::
2])
12.# Output: [1,
3, 5] (every
second
element)

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:

The count() method for a tuple is 1. fruits = ("apple",


used to count how many times a "banana", "apple",
count() "orange")
specified element appears in the
tuple. 2. print(fruits.count("
apple")) #Counts
the number of
times apple is
found in tuple.
3. #Output: 2

index() The index() method in a tuple is Syntax:


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

Syntax:

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


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

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:

Get the number of elements in the 1. fruits = ("apple",


len() "banana",
tuple using len().
"orange")
2. print(len(fruits))
#Returns length of
the tuple.
3. #Output: 3

You might also like