Dictionary Operations ===================== Dictionary Methods: ==================== How to define the dictionary in the run time: ============================================= eval() ======= -> to create any collection dynamically, we can use "eval()". Syntax: collection-name = eval(input("some text")) d = eval(input("Enter a dictionary:")) print("The Given dictionary = ",d) How to delete the data from the dictionary: ============================================ 1) pop(): ======== -> pop() can accept the key -> with the reference of the key, pop() can delete that specified value (value which is associated with the given key) Syntax: dictionary-name.pop(key) -> pop() can return a value which is deleting based on the key. d = eval(input("Enter a dictionary:")) print("The Given dictionary = ",d) x = d.pop('a') print("The Dictionary after the pop operation = ",d) print(x) d.pop('p') print(d) -> pop() can give a run time error "key error", when the specified key is not in the given dictionary. 2) popitem(): ============= -> popitem() not accept any key as parameter. -> it can able to delete the last item (key-value pair) from the dictionary. -> popitem() can return "that item (which is going to delete)" -> when we can define popitem() on empty dictionary, we can get "key error". d = {'apple':121,'banana':112,'cherry':123,'mango':1023} d1 = {} print("The Original Dictionary = ",d) x = d.popitem() # d1.popitem() # d1.pop(1) print("The Dictionary after the delete operation = ",d) print(x) print(d1) 3) del operator =============== -> del operator can use to delete the specific key-value pair. Syntax: del dictionary-name[key] -> del operator can also use to delete the whole dictionary permanently. Syntax: del dictional-name -> Once the deletion is completed, then, we cannot access it. If you can use to access, we can get "name error". d = {'apple':121,'banana':112,'cherry':123,'mango':1023} print("The Original Dictionary = ",d) del d['cherry'] print("The Dictionary after the delete operation is = ",d) del d print(d) 4) clear() ========== -> clear() can clear/delete/remove all key-value pairs from the dictionary. -> after the clear we can able to print an empty dictionary. Syntax: dictionary-name.clear() d = {'apple':121,'banana':112,'cherry':123,'mango':1023} print("The Original Dictionary = ",d) # d.clear('apple') d.clear() print(d) ============================================== items(): ======= Syntax: dictionary-name.items() -> items() can return list of tuples Each tuple is an arbitrary with key-value. d = {'apple':121,'banana':112,'cherry':123,'mango':1023} print("The Original Dictionary = ",d) x = d.items() print(x) for i in x: print(i) ================================================ keys(): ======= Syntax: dictionary-name.keys() -> to get only the keys from the dictionary, we can use "keys()". d = {'apple':121,'banana':112,'cherry':123,'mango':1023} print("The Original Dictionary = ",d) x = d.keys() print(x) for i in x: # print(i) # print(d[i]) print(i,":",d[i]) ============================================= values() ======== Syntax: dictionary-name.values() d = {'apple':121,'banana':112,'cherry':123,'mango':1023} print("The Original Dictionary = ",d) x = d.values() print(x) for i in x: print(i) =================================================== Comprehensions: ============== -> It is a shorter way/syntax which can be used to create a collection based on the values of existing collection. -> For immutable collections, comprehension is not allowed. -> So, there are three collections only in python which support the comprehensions. 1) List Comprehensions 2) Set Comprehensions 3) Dictionary Comprehensions 1) List Comprehensions ====================== Syntax: new_List = [Output_Expression for variable in Input_Expression if(variable satisfies the condition)] # creating a list from existing listData = ["apple","banana","cherry","kiwi","mango"] newList = [] # traditional way for i in listData: if 'a' in i: newList.append(i) print(newList) # using List comprehensions newList = [x for x in listData if "a" in x] print("The Original list = ",listData) print("The New List = ",newList) ==================== listData = [1,2,3,4,5,6,7,8] dictionary = {var:var ** 3 for var in listData if var % 2 != 0} print(dictionary)