Cheat Sheet Python Data Structure
Cheat Sheet Python Data Structure
Dictionaries
Package/Method Description Code Example
Example:
1. 1
A dictionary is a built-in data type that represents 2. 2
Creating a
a collection of key-value pairs. Dictionaries are
Dictionary 1. dict_name = {} #Creates an empty dictionary
enclosed in curly braces {}.
2. person = { "name": "John", "age": 30, "city": "New York"}
Copied!
Syntax:
1. 1
1. Value = dict_name["key_name"]
Copied!
You can access the values in a dictionary using
Accessing Values Example:
their corresponding keys.
1. 1
2. 2
1. name = person["name"]
2. age = person["age"]
Copied!
Syntax:
1. 1
1. dict_name[key] = value
Copied!
Inserts a new key-value pair into the dictionary. If
Add or modify the key already exists, the value will be updated; Example:
otherwise, a new entry is created.
1. 1
2. 2
Copied!
Syntax:
1. 1
1. del dict_name[key]
1. 1
1. del person["Country"]
Copied!
Syntax:
1. 1
1. dict_name.update({key: value})
1. 1
1. person.update({"Profession": "Doctor"})
Copied!
clear() The clear() method empties the dictionary, Syntax:
removing all key-value pairs within it. After this
operation, the dictionary is still accessible and can 1. 1
be used further. 1. dict_name.clear()
Copied!
Example:
1. 1
1. grades.clear()
about:blank 1/4
06/03/2024, 15:20 about:blank
Copied!
Example:
1. 1
You can check for the existence of a key in a 2. 2
key existence
dictionary using the in keyword 1. if "name" in person:
2. print("Name exists in the dictionary.")
Copied!
Syntax:
1. 1
1. new_dict = dict_name.copy()
1. new_person = person.copy()
2. new_person = dict(person) # another way to create a copy of dictionary
Copied!
Syntax:
1. 1
1. keys_list = list(dict_name.keys())
1. person_keys = list(person.keys())
Copied!
Syntax:
1. 1
1. values_list = list(dict_name.values())
1. person_values = list(person.values())
Copied!
Syntax:
1. 1
1. items_list = list(dict_name.items())
1. 1
1. info = list(person.items())
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
add()
automatically removed, as sets only store unique values. Example:
1. 1
1. fruits.add("mango")
Copied!
clear() The `clear()` method removes all elements from the set, resulting in an empty set. Syntax:
It updates the set in-place.
1. 1
1. set_name.clear()
about:blank 2/4
06/03/2024, 15:20 about:blank
Copied!
Example:
1. 1
1. fruits.clear()</td>
Copied!
Syntax:
1. 1
1. new_set = set_name.copy()
Copied!
The `copy()` method creates a shallow copy of the set. Any modifications to the
copy()
copy won't affect the original set. Example:
1. 1
1. new_fruits = fruits.copy()
Copied!
Example:
1. 1
A set is an unordered collection of unique elements. Sets are enclosed in curly 2. 2
Defining Sets braces `{}`. They are useful for storing distinct values and performing set
operations. 1. empty_set = set() #Creating an Empty
2. Set 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
discard()
the element is not found. Example:
1. 1
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 another set. It Copied!
issubset() returns True if 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:
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,
otherwise False. 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 Copied!
pop() raises a `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!
remove() Use the `remove()` method to remove a specific element from the set. Raises a Syntax:
`KeyError` if the element is not found.
1. 1
1. set_name.remove(element)
Copied!
about:blank 3/4
06/03/2024, 15:20 about:blank
Example:
1. 1
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)
Copied!
Perform various operations on sets: `union`, `intersection`, `difference`,
Set Operations
`symmetric 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
update()
maintains the uniqueness of elements. Example:
1. 1
1. fruits.update(["kiwi", "grape"])
Copied!
about:blank 4/4