0% found this document useful (0 votes)
13 views

Pujan's Python Student Activity

The document discusses properties of dictionary keys in Python, built-in dictionary methods and functions, iterating through dictionaries, and common dictionary functions. Dictionaries allow storing data in key-value pairs and are an important data structure for efficient data retrieval and flexible data storage.

Uploaded by

sarthakchua29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Pujan's Python Student Activity

The document discusses properties of dictionary keys in Python, built-in dictionary methods and functions, iterating through dictionaries, and common dictionary functions. Dictionaries allow storing data in key-value pairs and are an important data structure for efficient data retrieval and flexible data storage.

Uploaded by

sarthakchua29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Gujarat Technological University

Sal Institute of Diploma Studies[SIDS]

Department Of information Technology

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.

• Example: dict = {"name": "Alice", "age": 30, "city": "New York"}


items = my_dict.items()
print(items)
#output: dict_items([('name', 'Alice'), ('age', 30), ('city', 'New York')])

❖ Clear():
• The clear() method removes all items from the dictionary, leaving it empty

• Example: my_dict = {"name": "Alice", "age": 30, "city": "New York"}


my_dict.clear()
print(my_dict)
#output: {}
BUILT-IN DICTIONARY
METHODS
❖ Copy():
• The copy() method returns a shallow copy of the dictionary. This means that the new dictionary is a
separate object with the same key-value pairs as the original. If the values are mutable objects
• (like lists or other dictionaries), the references to these objects are copied, not the objects themselves.

• 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

• Iterating Through Values:


my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print("Values:")
for value in my_dict.values():
print(value)
#output: Values:
Alice
30
New York
ITERATING THROUGH
DICTIONARIES
❖ Iterating through keys, values, and key-value pairs

• Iterating Through Key-Value Pairs:


my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print("Key-Value Pairs:")
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")
#output: Key-Value Pairs:
Key: name, Value: Alice
Key: age, Value: 30
Key: city, Value: New York
COMMON DICTIONARY FUNCTIONS
▪ len(): The len() function returns the number of items in a container, such
as a list, tuple, string, or dictionary.
• Example :
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print("Length of the list:", length)
#output: Length of the list: 5

▪ Sorted():The sorted() function returns a sorted list of the specified


iterable, either in ascending or descending order. It does not modify the
original iterable.
• Example :
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5]
sorted_list = sorted(my_list)
print("Sorted list:", sorted_list)
#output: Sorted list: [1, 1, 2, 3, 4, 5, 5, 6, 9]
COMMON DICTIONARY FUNCTIONS
▪ Any() & All() :
• The any() function returns True if any element of the iterable is true. If the
iterable is empty, it returns False.

• The all() function returns True if all elements of the iterable are true. If the
iterable is empty, it returns True.

Example: my_list = [True, False, True, False]


any_result = any(my_list)
all_result = all(my_list)
print("Any result:", any_result)
print("All result:", all_result)
#Output:
Any result: True
All result: False

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

You might also like