estructura de datos Python modulo 2
estructura de datos Python modulo 2
Dictionaries
Package/Method Description Code Example
A dictionary
is a built-in
data type that Example:
represents a 1. 1
collection of 2. 2
Creating a
key-value
Dictionary 1. dict_name = {} #Creates an empty dictionary
pairs.
2. person = { "name": "John", "age": 30, "city": "New York"}
Dictionaries
are enclosed Copied!
in curly
braces {}.
Syntax:
1. 1
1. Value = dict_name["key_name"]
You can
access the Copied!
values in a
Accessing Values dictionary Example:
using their
corresponding 1. 1
keys. 2. 2
1. name = person["name"]
2. age = person["age"]
Copied!
Syntax:
Inserts a new 1. 1
key-value 1. dict_name[key] = value
pair into the
dictionary. If Copied!
the key
Add or modify already exists, Example:
the value will
be updated; 1. 1
otherwise, a 2. 2
new entry is 1. person["Country"] = "USA" # A new entry will be created.
created. 2. person["city"] = "Chicago" # Update the existing value for the same key
Copied!
del Removes the Syntax:
specified key-
value pair 1. 1
from the 1. del dict_name[key]
dictionary.
Raises a Copied!
about:blank 1/6
20/8/24, 11:54 about:blank
KeyError if Example:
the key does
not exist. 1. 1
1. del person["Country"]
Copied!
Syntax:
The update()
method 1. 1
merges the 1. dict_name.update({key: value})
provided
dictionary Copied!
update() into the
existing Example:
dictionary,
adding or 1. 1
updating key- 1. person.update({"Profession": "Doctor"})
value pairs.
Copied!
The clear() Syntax:
method
empties the 1. 1
dictionary,
removing all 1. dict_name.clear()
key-value
Copied!
pairs within
clear()
it. After this
Example:
operation, the
dictionary is 1. 1
still
accessible 1. grades.clear()
and can be
Copied!
used further.
Example:
You can
check for the 1. 1
existence of a 2. 2
key existence key in a
dictionary 1. if "name" in person:
2. print("Name exists in the dictionary.")
using the in
keyword Copied!
Creates a Syntax:
shallow copy
of the 1. 1
dictionary. 1. new_dict = dict_name.copy()
The new
dictionary Copied!
contains the
copy() same key- Example:
value pairs as
the original, 1. 1
but they 2. 2
remain 1. new_person = person.copy()
distinct 2. new_person = dict(person) # another way to create a copy of dictionary
objects in
memory. Copied!
about:blank 2/6
20/8/24, 11:54 about:blank
Syntax:
Retrieves all 1. 1
keys from the
dictionary 1. keys_list = list(dict_name.keys())
and converts Copied!
them into a
keys()
list. Useful
Example:
for iterating
or processing 1. 1
keys using
list methods. 1. person_keys = list(person.keys())
Copied!
Syntax:
Extracts all 1. 1
values from
the dictionary 1. values_list = list(dict_name.values())
and converts Copied!
them into a
values()
list. This list Example:
can be used
for further 1. 1
processing or
analysis. 1. person_values = list(person.values())
Copied!
Syntax:
Retrieves all
key-value 1. 1
pairs as tuples 1. items_list = list(dict_name.items())
and converts
them into a Copied!
items() list of tuples.
Each tuple Example:
consists of a
key and its 1. 1
corresponding 1. info = list(person.items())
value.
Copied!
Sets
Package/Method Description Code Example
add() Elements can be added to a set using the Syntax:
`add()` method. Duplicates are
1. 1
automatically removed, as sets only
store unique values. 1. set_name.add(element)
Copied!
Example:
1. 1
1. fruits.add("mango")
about:blank 3/6
20/8/24, 11:54 about:blank
Copied!
Syntax:
1. 1
1. set_name.clear()
1. 1
1. fruits.clear()</td>
Copied!
Syntax:
1. 1
1. new_set = set_name.copy()
1. 1
1. new_fruits = fruits.copy()
Copied!
Example:
A set is an unordered collection of 1. 1
unique elements. Sets are enclosed in 2. 2
Defining Sets curly braces `{}`. They are useful for
storing distinct values and performing 1. empty_set = set() #Creating an Empty
2. Set fruits = {"apple", "banana", "orange"}
set operations.
Copied!
Syntax:
1. 1
1. set_name.discard(element)
1. 1
1. fruits.discard("apple")
Copied!
issubset() The `issubset()` method checks if the Syntax:
current set is a subset of another set. It
1. 1
returns True if all elements of the
current set are present in the other set, 1. is_subset = set1.issubset(set2)
otherwise False.
Copied!
Example:
about:blank 4/6
20/8/24, 11:54 about:blank
1. 1
1. is_subset = fruits.issubset(colors)
Copied!
Syntax:
is_superset = set1.issuperset(set2)
The `issuperset()` method checks if the
current set is a superset of another set. It Example:
issuperset() returns True if all elements of the other
set are present in the current set, 1. 1
otherwise False.
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 Copied!
pop() raises a `KeyError` if the set is empty.
Use this method to remove elements Example:
when the order doesn't matter.
1. 1
1. removed_fruit = fruits.pop()
Copied!
Syntax:
1. 1
1. set_name.remove(element)
1. fruits.remove("banana")
Copied!
Set Operations Perform various operations on sets: Syntax:
`union`, `intersection`, `difference`,
1. 1
`symmetric difference`.
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!
Example:
1. 1
2. 2
about:blank 5/6
20/8/24, 11:54 about:blank
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)
1. 1
1. fruits.update(["kiwi", "grape"])
Copied!
about:blank 6/6