Dictionary in Python-1
Dictionary in Python-1
• List_ in_list = [
• [1, 2, 3],
• [4, 5, 6],
• [7, 8, 9]
•]
• # Accessing elements
• print(List_ in_list [0][0]) # Output: 1
• print(List_ in_list [1][2]) # Output: 6
• Return Value: The index() function returns the index of the first
occurrence of the specified value. If the value is not found, it raises a
ValueError.
• my_list = [1, 2, 3, 4, 3, 5]
• print(my_list.index(3)) # Output: 2
• print(my_list.index(3, 3, 6)) # Output: 4
• # print(my_list.index(6)) # Raises ValueError
• my_tuple = ('a', 'b', 'c', 'd', 'b')
• print(my_tuple.index('b')) # Output: 1
• print(my_tuple.index('b', 2, 5)) # Output: 4
• # print(my_tuple.index('z')) # Raises ValueError
• my_string = "hello world"
• print(my_string.index('o')) # Output: 4
• print(my_string.index('o', 5)) # Output: 7
• print(my_string.index('world')) # Output: 6
• # print(my_string.index('x')) # Raises ValueError
can i pass a list that contains two list as argument in set() in not then why?
• You cannot pass a list that contains other lists as an argument to the
set() function in Python. This is because sets require their elements to
be hashable, and lists are mutable and therefore not hashable.
• Explanation
• Hashable Objects: In Python, an object is hashable if it has a hash
value that does not change during its lifetime. Hashable objects can
be compared to each other and used as keys in a dictionary or as
elements of a set.
• Mutable Objects: Lists in Python are mutable, meaning their contents
can change after they are created. This mutability means they do not
have a fixed hash value and are therefore not hashable.
• list_of_lists = [[1, 2], [3, 4]]
• my_set = set(list_of_lists) # This will raise a TypeError
• Solution
• If you need to create a set from a list of lists, you can convert the
inner lists to tuples (which are immutable and hashable) before
passing them to the set() function:
• list_of_lists = [[1, 2], [3, 4]]
• list_of_tuples = [tuple(inner_list) for inner_list in list_of_lists]
• my_set = set(list_of_tuples)
#Modifying a value
• My_dict[“age”] = 31
• print(My_dict) # {'name': ‘Ram', 'age': 31, 'City': 'new York', 'job': 'Engineer’}
• A = "name" in d1
• print(A) Output: True
• #Iterating over keys and values
For key, value in My_dict.items():
print(f”{key} : {value}”)
# using a dictionary comprehension
Squares = {x: x*x for x in range(5)}
print(Squares)
Merging Dictionaries (Python 3.9+)
• dict1 = {'a': 1, 'b': 2}
• dict2 = {'b': 3, 'c': 4}
• merged_dict = dict1 | dict2
• print(merged_dict)
• # Deep copy(for deep copy you need to import copy, for shallow we
not need to import anything)
• import copy
• deep_copy_dict = copy.deepcopy(my_dict)
• In Python, the difference between shallow and deep copy is how they
handle copying of nested objects. Let's break down the concepts and
see some examples to illustrate the difference.
• Shallow Copy
• A shallow copy of an object creates a new object, but inserts references
into it to the objects found in the original. This means that the copy is
only one level deep, and any changes to nested objects within the
copied object will reflect in the original object, and vice versa.
• Deep Copy
• A deep copy of an object creates a new object and recursively copies all
objects found in the original. This means that the copy is completely
independent of the original, and changes to nested objects within the
copied object will not affect the original object, and vice versa.
• import copy
• Output:
• {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Pop() function
• In Python, the pop() method in dictionaries is used to remove a
specified key and return its corresponding value. If the specified key is
not found, the method can return a default value if provided, or raise
a KeyError if no default value is specified.
• dict.pop(key,[ default])
• # Attempt to remove and return the value for a non-existing key 'd' with a
default value
• value = my_dict.pop('d', 'Key not found')
• print(value) # Output: Key not found
• # Attempt to remove and return the value for a non-existing key 'd' with a default value
• value = my_dict.pop('d')
• print(value) # Output: Key not found