Sets and Dictionaries
Sets and Dictionaries
Dictionaries
Package/Method Description Code Example
Example:
1. 1
A dictionary is a built-in data type that represents a 2. 2
Creating a
collection of key-value pairs. Dictionaries are enclosed in
Dictionary 1. dict_name = {} #Creates an empty dictionary
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 their
Accessing Values Example:
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 the key
Add or modify already exists, the value will be updated; otherwise, a Example:
new entry is created.
1. 1
2. 2
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. dict_name.update({key: value})
1. 1
1. person.update({"Profession": "Doctor"})
Copied!
clear() The clear() method empties the dictionary, removing all Syntax:
key-value pairs within it. After this operation, the
dictionary is still accessible and can be used further. 1. 1
1. dict_name.clear()
Copied!
Example:
1. 1
1. grades.clear()
about:blank 1/4
5/9/24, 7:12 PM about:blank
Copied!
Example:
1. 1
You can check for the existence of a key in a dictionary 2. 2
key existence
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()
Copied!
Creates a shallow copy of the dictionary. The new
copy() dictionary contains the same key-value pairs as the Example:
original, but they 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!
Syntax:
1. 1
1. keys_list = list(dict_name.keys())
Retrieves all keys from the dictionary and converts them Copied!
keys() into a list. Useful for iterating or processing keys using
list methods. Example:
1. 1
1. person_keys = list(person.keys())
Copied!
Syntax:
1. 1
1. values_list = list(dict_name.values())
Extracts all values from the dictionary and converts them Copied!
values() into a list. This list can be used for further processing or
analysis. Example:
1. 1
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 automatically
add()
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. It Syntax:
updates the set in-place.
1. 1
1. set_name.clear()
about:blank 2/4
5/9/24, 7:12 PM 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
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
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 the
discard()
element 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
issubset()
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 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!
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
5/9/24, 7:12 PM 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`, `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
update()
the uniqueness of elements. Example:
1. 1
1. fruits.update(["kiwi", "grape"])
Copied!
about:blank 4/4