Pujan's Python Student Activity
Pujan's Python Student Activity
Semester:-2
Academic Year :-2024-2025
Properties of Dictionary Keys ,
Built-In Dictionary Methods and functions
Guided by Prepared by
Dhara Prajapati Desai Pujan - 234510316009
❑ CONTENTS :
• Introduction to dictionaries
• Properties of Dictionary keys
• Built-In Dictionary Methods
• Iterating Through Dictionaries
• Common Dictionary Functions
INTRODUCTION TO
DICTIONARIES
❖ Definition and importance of dictionaries in Python:
➢ a dictionary is a built-in data structure that allows you to store data in key-
value pairs. It is a mutable, unordered collection of items where each item is
identified by a unique key. Dictionaries are created using curly braces’{}’with
key-value pairs separated by colons ‘:’ and pairs separated by commas.
Example of dictionaries
my_dict = {
"name": "Alice",
"age": 30,
"city": "New York"
}
INTRODUCTION TO
DICTIONARIES
❖ Importance of Dictionaries in python
• Efficient Data Retrieval: Dictionaries provide an efficient way to store and
retrieve data. Unlike lists, where you access elements by their index, dictionaries
allow you to access data by keys, making lookups fast and intuitive.
• Flexibility in Data Storage: Dictionaries can store a wide variety of data types as
values, including lists, other dictionaries, and even functions. This flexibility
makes them suitable for representing complex data structures.
• Unordered Collections: Since dictionaries are unordered collections, the order of
elements is not preserved, which is often useful when the order of data does not
matter and you prioritize quick access and modification of data.
• Mutable Nature: Dictionaries are mutable, meaning you can change, add, or
remove key-value pairs after the dictionary has been created. This mutability is
crucial for tasks that require dynamic data management.
• Clear and Readable Code: Using dictionaries can make your code more readable
and expressive, especially when dealing with large datasets or configurations. For
example, instead of using multiple lists to represent related data, a single
dictionary can be used with descriptive keys.
PROPERTIES OF
DICTIONARY KEYS
❖ Uniqueness of Keys in Python Dictionaries:
• In Python dictionaries, keys must be unique. This means that
within a single dictionary, you cannot have two items with the
same key. If you attempt to insert a duplicate key, the new value
will overwrite the existing value associated with that key. This
property ensures that each key maps to exactly one value.
❖ Why Keys Must Be Unique:
1.Direct Access: The primary function of a dictionary is to allow for the direct access of values
using keys. If keys were not unique, the direct access mechanism would be compromised, as
it would be unclear which value to retrieve for a given key.
2.Efficient Lookups: Dictionaries are implemented using hash tables. Each key is hashed to a
unique index, which allows for efficient lookups. Duplicate keys would disrupt the hashing
process, leading to potential conflicts and inefficiencies.
3.Data Integrity: Ensuring keys are unique helps maintain data integrity. When each key
uniquely identifies a value, there is less risk of data being inadvertently overwritten or lost
BUILT-IN DICTIONARY
METHODS
❖ Keys():
• The keys() method returns a view object that displays a list of all the keys in the dictionary. The
view object is dynamic, meaning it reflects changes made to the dictionary.
• Example:
dict = {"name": "Alice", "age": 30, "city": "New York"}
keys = dict.keys()
print(keys)
#output: dict_keys(['name', 'age', 'city'])
❖ Values():
• The values() method returns a view object that displays a list of all the values in the dictionary.
The view object is dynamic.
• Example:
dict = {"name": "Alice", "age": 30, "city": "New York"}
values = dict.values()
print(values)
#output: dict_values(['Alice', 30, 'New York'])
BUILT-IN DICTIONARY
METHODS
❖ Items():
• The items() method returns a view object that displays a list of the dictionary's key-value pairs as
tuples. This view object is dynamic.
❖ Clear():
• The clear() method removes all items from the dictionary, leaving it empty
• Example:
original_dict = {"name": "Alice", "age": 30, "city": "New York"} copied_dict = original_dict.copy()
print(copied_dict)
• #output:{'name': 'Alice', 'age': 30, 'city': 'New York’}
copied_dict["age"] = 31
print(original_dict)
print(copied_dict)
#output: {'name': 'Alice', 'age': 30, 'city': 'New York’}
#output: {'name': 'Alice', 'age': 31, 'city': 'New York'}
ITERATING THROUGH
DICTIONARIES
❖ Iterating through keys, values, and key-value pairs
• Iterating Through Keys:
my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print("Keys:")
for key in my_dict.keys():
print(key)
#output:
Keys:
name
age
city
ITERATING THROUGH
DICTIONARIES
❖ Iterating through keys, values, and key-value pairs
• The all() function returns True if all elements of the iterable are true. If the
iterable is empty, it returns True.
In this example, any() returns True because at least one element in my_list is
True, while all() returns False because not all elements in my_list are True.
THANK YOU