structures
structures
Dictionaries
Package/
Description Code Example
Method
A dictionary
is a built-in Example:
data type that
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 Example:
dictionary
Values
using their
1. 1
corresponding 2. 2
keys.
1. name = person["name"]
2. age = person["age"]
Copied!
Syntax:
1. 1
Inserts a new
key-value 1. dict_name[key] = value
pair into the
dictionary. If Copied!
the key
Add or
already exists, Example:
modify
the value will
1. 1
be updated; 2. 2
otherwise, a
new entry is 1. person["Country"] = "USA" # A new entry will be created.
2. person["city"] = "Chicago" # Update the existing value for the same key
created.
Copied!
Syntax:
1. 1
Removes the
specified key- 1. del dict_name[key]
value pair
Copied!
from the
del dictionary.
Raises a Example:
KeyError if 1. 1
the key does
not exist. 1. del person["Country"]
Copied!
Syntax:
The update() 1. 1
method
merges the 1. dict_name.update({key: value})
provided
Copied!
dictionary
update() into the
existing Example:
dictionary, 1. 1
adding or
updating key- 1. person.update({"Profession": "Doctor"})
value pairs.
Copied!
Syntax:
Retrieves all 1. 1
keys from the 1. keys_list = list(dict_name.keys())
dictionary
and converts Copied!
them into a
keys()
list. Useful Example:
for iterating
or processing 1. 1
keys using 1. person_keys = list(person.keys())
list methods.
Copied!
Syntax:
Extracts all 1. 1
values from 1. values_list = list(dict_name.values())
the dictionary
and converts Copied!
them into a
values()
list. This list Example:
can be used
for further 1. 1
processing or 1. person_values = list(person.values())
analysis.
Copied!
Sets
Package/
Description Code Example
Method
Syntax:
1. 1
1. set_name.add(element)
Elements can be added to a set
Copied!
using the `add()` method.
add() Duplicates are automatically
Example:
removed, as sets only store
unique values. 1. 1
1. fruits.add("mango")
Copied!
Syntax:
1. 1
1. set_name.clear()
1. fruits.clear()</td>
Copied!
Syntax:
1. 1
1. new_set = set_name.copy()
1. new_fruits = fruits.copy()
Copied!
Copied!
Syntax:
1. 1
1. set_name.discard(element)
1. fruits.discard("apple")
Copied!
Syntax:
1. 1
1. is_subset = set1.issubset(set2)
The `issubset()` method checks
if the current set is a subset of Copied!
another set. It returns True if all
issubset()
elements of the current set are Example:
present in the other set,
otherwise False. 1. 1
1. is_subset = fruits.issubset(colors)
Copied!
Syntax:
Copied!
Example:
1. 1
1. removed_fruit = fruits.pop()
Copied!
Syntax:
1. 1
1. set_name.remove(element)
1. fruits.remove("banana")
Copied!
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)
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!
Copied!
Example:
1. 1
1. fruits.update(["kiwi", "grape"])
Copied!