Python listas y duplas
Python listas y duplas
List
Package/Method Description Code Example
Syntax:
1. 1
1. list_name.append(element)
Copied!
The `append()` method is used to add an element to the
append() Example:
end of a list.
1. 1
2. 2
Copied!
Example 1:
1. 1
2. 2
3. 3
The `copy()` method is used to create a shallow copy of a
copy()
list. 1. my_list = [1, 2, 3, 4, 5]
2. new_list = my_list.copy() print(new_list)
3. # Output: [1, 2, 3, 4, 5]
Copied!
Example:
1. 1
2. 2
3. 3
The `count()` method is used to count the number of
count()
occurrences of a specific element in a list in Python. 1. my_list = [1, 2, 2, 3, 4, 2, 5, 2]
2. count = my_list.count(2) print(count)
3. # Output: 4
Copied!
Example:
A list is a built-in data type that represents an ordered
1. 1
and mutable collection of elements. Lists are enclosed in
Creating a list
square brackets [] and elements are separated by 1. fruits = ["apple", "banana", "orange", "mango"]
commas.
Copied!
Example:
1. 1
2. 2
The `del` statement is used to remove an element from 3. 3
del list. `del` statement removes the element at the specified
index. 1. my_list = [10, 20, 30, 40, 50]
2. del my_list[2] # Removes the element at index 2 print(my_list)
3. # Output: [10, 20, 40, 50]
Copied!
Syntax:
1. 1
1. list_name.extend(iterable)
Copied!
Copied!
Indexing Indexing in a list allows you to access individual Example:
elements by their position. In Python, indexing starts
1. 1
from 0 for the first element and goes up to 2. 2
`length_of_list - 1`. 3. 3
about:blank 1/7
14/8/24, 14:08 about:blank
4. 4
5. 5
Copied!
Syntax:
1. 1
1. list_name.insert(index, element)
Copied!
Example:
insert() The `insert()` method is used to insert an element.
1. 1
2. 2
3. 3
1. my_list = [1, 2, 3, 4, 5]
2. my_list.insert(2, 6)
3. print(my_list)
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
You can use indexing to modify or assign new values to
Modifying a list
specific elements in the list. 1. my_list = [10, 20, 30, 40, 50]
2. my_list[1] = 25 # Modifying the second element
3. print(my_list)
4. # Output: [10, 25, 30, 40, 50]
Copied!
Example 1:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
Copied!
Example:
1. 1
2. 2
3. 3
To remove an element from a list. The `remove()` 4. 4
remove() method removes the first occurrence of the specified
value. 1. my_list = [10, 20, 30, 40, 50]
2. my_list.remove(30) # Removes the element 30
3. print(my_list)
4. # Output: [10, 20, 40, 50]
Copied!
about:blank 2/7
14/8/24, 14:08 about:blank
Example 1:
1. 1
2. 2
3. 3
The `reverse()` method is used to reverse the order of
reverse()
elements in a list 1. my_list = [1, 2, 3, 4, 5]
2. my_list.reverse() print(my_list)
3. # Output: [5, 4, 3, 2, 1]
Copied!
Syntax:
1. 1
1. list_name[start:end:step]
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
You can use slicing to access a range of elements from a 8. 8
Slicing 9. 9
list.
10. 10
11. 11
12. 12
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)
Copied!
Example 1:
1. 1
2. 2
3. 3
4. 4
1. my_list = [5, 2, 8, 1, 9]
2. my_list.sort()
3. print(my_list)
4. # Output: [1, 2, 5, 8, 9]
The `sort()` method is used to sort the elements of a list Copied!
in ascending order. If you want to sort the list in
sort()
descending order, you can pass the `reverse=True` Example 2:
argument to the `sort()` method.
1. 1
2. 2
3. 3
4. 4
1. my_list = [5, 2, 8, 1, 9]
2. my_list.sort(reverse=True)
3. print(my_list)
4. # Output: [9, 8, 5, 2, 1]
Copied!
Dictionary
Package/Method Description Code Example
Accessing Values You can access the values in a dictionary using their Syntax:
corresponding `keys`.
1. 1
1. Value = dict_name["key_name"]
Copied!
Example:
1. 1
2. 2
1. name = person["name"]
2. age = person["age"]
about:blank 3/7
14/8/24, 14:08 about:blank
Copied!
Syntax:
1. 1
1. dict_name[key] = value
Copied!
Inserts a new key-value pair into the dictionary. If the key
Add or modify already exists, the value will be updated; otherwise, a new entry Example:
is created.
1. 1
2. 2
Copied!
Syntax:
1. 1
1. dict_name.clear()
The `clear()` method empties the dictionary, removing all key- Copied!
clear() value pairs within it. After this operation, the dictionary is still
accessible and can be used further. Example:
1. 1
1. grades.clear()
Copied!
Syntax:
1. 1
1. new_dict = dict_name.copy()
Copied!
Creates a shallow copy of the dictionary. The new dictionary
copy() contains the same key-value pairs as the original, but they Example:
remain distinct objects in memory.
1. 1
2. 2
1. new_person = person.copy()
2. new_person = dict(person) # another way to create a copy of dictionary
Copied!
Example:
1. 1
A dictionary is a built-in data type that represents a collection 2. 2
Creating a
of key-value pairs. Dictionaries are enclosed in curly braces
Dictionary 1. dict_name = {} #Creates an empty dictionary
`{}`.
2. person = { "name": "John", "age": 30, "city": "New York"}
Copied!
Syntax:
1. 1
1. del dict_name[key]
Copied!
Removes the specified key-value pair from the dictionary.
del
Raises a `KeyError` if the key does not exist. Example:
1. 1
1. del person["Country"]
Copied!
Syntax:
1. 1
1. items_list = list(dict_name.items())
Retrieves all key-value pairs as tuples and converts them into a Copied!
items() list of tuples. Each tuple consists of a key and its corresponding
value. Example:
1. 1
1. info = list(person.items())
Copied!
key existence You can check for the existence of a key in a dictionary using Example:
the `in` keyword
1. 1
about:blank 4/7
14/8/24, 14:08 about:blank
2. 2
1. if "name" in person:
2. print("Name exists in the dictionary.")
Copied!
Syntax:
1. 1
1. keys_list = list(dict_name.keys())
Copied!
Retrieves all keys from the dictionary and converts them into a
keys()
list. Useful for iterating or processing keys using list methods. Example:
1. 1
1. person_keys = list(person.keys())
Copied!
Syntax:
1. 1
1. dict_name.update({key: value})
Copied!
The `update()` method merges the provided dictionary into the
update()
existing dictionary, adding or updating key-value pairs. Example:
1. 1
1. person.update({"Profession": "Doctor"})
Copied!
Syntax:
1. 1
1. values_list = list(dict_name.values())
Copied!
Extracts all values from the dictionary and converts them into a
values()
list. This list can be used for further processing or analysis. Example:
1. 1
1. person_values = list(person.values())
Copied!
Sets
Package/Method Description Code Example
Syntax:
1. 1
1. set_name.add(element)
Copied!
Elements can be added to a set using the `add()` method. Duplicates are automatically
add()
removed, as sets only store unique values. Example:
1. 1
1. fruits.add("mango")
Copied!
Syntax:
1. 1
1. set_name.clear()
Copied!
The `clear()` method removes all elements from the set, resulting in an empty set. It updates
clear()
the set in-place. Example:
1. 1
1. fruits.clear()
Copied!
copy() The `copy()` method creates a shallow copy of the set. Any modifications to the copy won't Syntax:
affect the original set.
1. 1
1. new_set = set_name.copy()
Copied!
about:blank 5/7
14/8/24, 14:08 about:blank
Example:
1. 1
1. new_fruits = fruits.copy()
Copied!
Example:
1. 1
2. 2
A set is an unordered collection of unique elements. Sets are enclosed in curly braces `{}`.
Defining Sets
They are useful for storing distinct values and performing set operations. 1. empty_set = set() #Creating an Empty Set
2. fruits = {"apple", "banana", "orange"}
Copied!
Syntax:
1. 1
1. set_name.discard(element)
Copied!
Use the `discard()` method to remove a specific element from the set. Ignores if the element
discard()
is not found. Example:
1. 1
1. fruits.discard("apple")
Copied!
Syntax:
1. 1
1. is_subset = set1.issubset(set2)
Copied!
The `issubset()` method checks if the current set is a subset of another set. It returns True if
issubset()
all elements of the current set are present in the other set, otherwise False. Example:
1. 1
1. is_subset = fruits.issubset(colors)
Copied!
Syntax:
1. 1
1. is_superset = set1.issuperset(set2)
Copied!
The `issuperset()` method checks if the current set is a superset of another set. It returns True
issuperset()
if all elements of the other set are present in the current set, otherwise False. Example:
1. 1
1. is_superset = colors.issuperset(fruits)
Copied!
Syntax:
1. 1
1. removed_element = set_name.pop()
The `pop()` method removes and returns an arbitrary element from the set. It raises a Copied!
pop() `KeyError` if the set is empty. Use this method to remove elements when the order doesn't
matter. Example:
1. 1
1. removed_fruit = fruits.pop()
Copied!
Syntax:
1. 1
1. set_name.remove(element)
Copied!
Use the `remove()` method to remove a specific element from the set. Raises a `KeyError` if
remove()
the element is not found. Example:
1. 1
1. fruits.remove("banana")
Copied!
about:blank 6/7
14/8/24, 14:08 about:blank
Syntax:
1. 1
2. 2
3. 3
4. 4
1. union_set = set1.union(set2)
2. intersection_set = set1.intersection(set2)
3. difference_set = set1.difference(set2)
4. sym_diff_set = set1.symmetric_difference(set2)
Copied!
Perform various operations on sets: `union`, `intersection`, `difference`, `symmetric
Set Operations
difference`. Example:
1. 1
2. 2
3. 3
4. 4
1. combined = fruits.union(colors)
2. common = fruits.intersection(colors)
3. unique_to_fruits = fruits.difference(colors)
4. sym_diff = fruits.symmetric_difference(colors)
Copied!
Syntax:
1. 1
1. set_name.update(iterable)
Copied!
The `update()` method adds elements from another iterable into the set. It maintains the
update()
uniqueness of elements. Example:
1. 1
1. fruits.update(["kiwi", "grape"]
Copied!
about:blank 7/7