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

Dictionary

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

Dictionary

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

Dictionary

Dictionaries in Python are unordered collections of items, where each item


consists of a key-value pair. Dictionaries are mutable, meaning they can be
modified after creation.

 A dictionary is a collection of key-value pairs.


 Keys within a dictionary must be unique and immutable (such as strings,
numbers, or tuples).
 Values within a dictionary can be of any data type (including other
dictionaries).
 Dictionaries are enclosed within curly braces {} and each key-value pair
is separated by a colon :

Creating a Dictionary:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
empty_dict = {}

Accessing Elements:
print(my_dict['name']) # Output: John
print(my_dict.get('age')) # Output: 30

Modifying a Dictionary
my_dict['age'] = 35 # Update value
my_dict['gender'] = 'Male' # Add new key-value pair

Removing Elements:
del my_dict['city'] # Remove a specific key-value pair
my_dict.pop('age') # Remove and return the value associated with the given
key
my_dict.clear() # Remove all items

Dictionary Methods:
# Getting keys and values
print(my_dict.keys())
print(my_dict.values())

# Checking membership
print('name' in my_dict) # Output: True

# Length of dictionary
print(len(my_dict))

Dictionary Iteration
# Iterating over keys
for key in my_dict:
print(key, my_dict[key])

# Iterating over key-value pairs


for key, value in my_dict.items():
print(key, value)

#Using built in functions on dictionaries:


Len(dict1) min(dict1) sorted(dict1) sum(dict1)key numbers
Max(dict1) reversed(dict1)
Nested Dictionaries:
nested_dict = {'person': {'name': 'John', 'age': 30}}
print(nested_dict['person']['age']) # Output: 30

1)clear(): Removes all items from the dictionary.


my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear()
print(my_dict) # Output: {}
This clears all items from the dictionary, leaving it empty.

2) copy(): Returns a shallow copy of the dictionary.


my_dict = {'a': 1, 'b': 2, 'c': 3}
new_dict = my_dict.copy()
print(new_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
his creates a shallow copy of the dictionary my_dict and assigns it to new_dict.

3) fromkeys(seq[, value]): Returns a new dictionary with keys from seq and
value set to value
keys = ['a', 'b', 'c']
values = 0
new_dict = dict.fromkeys(keys, values)
print(new_dict) # Output: {'a': 0, 'b': 0, 'c': 0}
: This creates a new dictionary with keys from the given sequence keys and
assigns the same value (0) to each key.

4) get(key[, default]): Returns the value for the specified key, or default if the
key is not found.
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.get('b')
print(value) # Output: 2
This retrieves the value associated with the key 'b' from the dictionary. If the
key is not found, it returns the default value 0.

5) items(): Returns a view object that displays a list of a dictionary's key-value


pairs as tuples.
my_dict = {'a': 1, 'b': 2, 'c': 3}
items = my_dict.items()
print(items) # Output: dict_items([('a', 1), ('b', 2), ('c', 3)])
This returns a view object containing tuples of key-value pairs from the
dictionary.

6) keys(): Returns a view object that displays a list of all the keys in the
dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.keys()
print(keys) # Output: dict_keys(['a', 'b', 'c'])
This returns a view object containing all the keys in the dictionary.

7) popitem(): Removes the last inserted key-value pair.


my_dict = {'a': 1, 'b': 2, 'c': 3}
item = my_dict.popitem()
print(item) # Output: ('c', 3)
print(my_dict) # Output: {'a': 1, 'b': 2}
8) update([other]): Updates the dictionary with the key-value pairs from another
dictionary or iterable.
my_dict = {'a': 1, 'b': 2}
other_dict = {'b': 3, 'c': 4}
my_dict.update(other_dict)
print(my_dict) # Output: {'a': 1, 'b': 3, 'c': 4}

9)pop(): In Python, dictionaries have a pop() method that allows you to remove
an item from the dictionary based on its key and returns the corresponding value

my_dict = {'a': 1, 'b': 2, 'c': 3}

# Remove 'b' from the dictionary and get its value


value = my_dict.pop('b')
print(value) # Output: 2
print(my_dict)

You might also like