05/07/2025, 19:03 about:blank
Cheat Sheet: Python Data Structures Part-2
Dictionaries
Package/Method Description Code Example
Example:
dict_name = {} #Creates an empty dictionary
person = { "name": "John", "age": 30, "city": "New York"}
Creating a A dictionary is a built-in data type that represents a collection of key-
Dictionary value pairs. Dictionaries are enclosed in curly braces {}.
Syntax:
Value = dict_name["key_name"]
You can access the values in a dictionary using their corresponding
Accessing Values
keys. Example:
name = person["name"]
age = person["age"]
Syntax:
dict_name[key] = value
Inserts a new key-value pair into the dictionary. If the key already exists,
Add or modify Example:
the value will be updated; otherwise, a new entry is created.
person["Country"] = "USA" # A new entry will be created.
person["city"] = "Chicago" # Update the existing value for the same key
del Removes the specified key-value pair from the dictionary. Raises a Syntax:
KeyError if the key does not exist.
del dict_name[key]
Example:
del person["Country"]
about:blank 1/6
05/07/2025, 19:03 about:blank
Syntax:
dict_name.update({key: value})
The update() method merges the provided dictionary into the existing
update()
dictionary, adding or updating key-value pairs.
Example:
person.update({"Profession": "Doctor"})
Syntax:
dict_name.clear()
The clear() method empties the dictionary, removing all key-value
clear() pairs within it. After this operation, the dictionary is still accessible and
can be used further. Example:
grades.clear()
Example:
if "name" in person:
print("Name exists in the dictionary.")
You can check for the existence of a key in a dictionary using the in
key existence
keyword
Syntax:
new_dict = dict_name.copy()
Creates a shallow copy of the dictionary. The new dictionary contains the
copy() same key-value pairs as the original, but they remain distinct objects in Example:
memory.
new_person = person.copy()
new_person = dict(person) # another way to create a copy of dictionary
about:blank 2/6
05/07/2025, 19:03 about:blank
Syntax:
keys_list = list(dict_name.keys())
Retrieves all keys from the dictionary and converts them into a list.
keys()
Useful for iterating or processing keys using list methods.
Example:
person_keys = list(person.keys())
Syntax:
values_list = list(dict_name.values())
Extracts all values from the dictionary and converts them into a list. This
values()
list can be used for further processing or analysis. Example:
person_values = list(person.values())
Syntax:
items_list = list(dict_name.items())
Retrieves all key-value pairs as tuples and converts them into a list of
items()
tuples. Each tuple consists of a key and its corresponding value. Example:
info = list(person.items())
Sets
Package/Method Description Code Example
add() Elements can be added to a set using the `add()` method. Duplicates are automatically removed, as sets only Syntax:
store unique values.
set_name.add(element)
Example:
about:blank 3/6
05/07/2025, 19:03 about:blank
fruits.add("mango")
Syntax:
set_name.clear()
clear() The `clear()` method removes all elements from the set, resulting in an empty set. It updates the set in-place.
Example:
fruits.clear()
Syntax:
new_set = set_name.copy()
The `copy()` method creates a shallow copy of the set. Any modifications to the copy won't affect the original
copy()
set.
Example:
new_fruits = fruits.copy()
Example:
empty_set = set() #Creating an Empty Set
fruits = {"apple", "banana", "orange"}
colors = ("orange", "red", "green")
A set is an unordered collection of unique elements. Sets are enclosed in curly braces `{}`. They are useful for
Defining Sets
storing distinct values and performing set operations.
Note: These two sets will be used in the examples that follow.
discard() Use the `discard()` method to remove a specific element from the set. Ignores if the element is not found. Syntax:
set_name.discard(element)
Example:
fruits.discard("apple")
about:blank 4/6
05/07/2025, 19:03 about:blank
Syntax:
is_subset = set1.issubset(set2)
The `issubset()` method checks if the current set is a subset of another set. It returns True if all elements of the
issubset()
current set are present in the other set, otherwise False.
Example:
is_subset = fruits.issubset(colors)
Syntax:
is_superset = set1.issuperset(set2)
The `issuperset()` method checks if the current set is a superset of another set. It returns True if all elements of
issuperset()
the other set are present in the current set, otherwise False.
Example:
is_superset = colors.issuperset(fruits)
Syntax:
removed_element = set_name.pop()
The `pop()` method removes and returns an arbitrary element from the set. It raises a `KeyError` if the set is
pop()
empty. Use this method to remove elements when the order doesn't matter.
Example:
removed_fruit = fruits.pop()
remove() Use the `remove()` method to remove a specific element from the set. Raises a `KeyError` if the element is not Syntax:
found.
set_name.remove(element)
about:blank 5/6
05/07/2025, 19:03 about:blank
Example:
fruits.remove("banana")
Syntax:
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)
sym_diff_set = set1.symmetric_difference(set2)
Set Operations Perform various operations on sets: `union`, `intersection`, `difference`, `symmetric difference`.
Example:
combined = fruits.union(colors)
common = fruits.intersection(colors)
unique_to_fruits = fruits.difference(colors)
sym_diff = fruits.symmetric_difference(colors)
Syntax:
set_name.update(iterable)
update() The `update()` method adds elements from another iterable into the set. It maintains the uniqueness of elements.
Example:
fruits.update(["kiwi", "grape"])
© IBM Corporation. All rights reserved.
about:blank 6/6