|
| 1 | +#https://newdigitals.org/2024/01/23/basic-python-programming/#dictionaries |
| 2 | +#Dictionaries are used to store data values in key: value pairs. |
| 3 | +#A dictionary is a collection which is ordered (as of Python version 3.7), changeable and do not allow duplicates. |
| 4 | +#Dictionaries are written with curly brackets, and have keys and values: |
| 5 | +thisdict = { |
| 6 | + "brand": "Ford", |
| 7 | + "model": "Mustang", |
| 8 | + "year": 1964 |
| 9 | +} |
| 10 | +print(thisdict) |
| 11 | +Output: |
| 12 | +{'brand': 'Ford', 'model': 'Mustang', 'year': 1964} |
| 13 | +#It is possible to use the dict() constructor to make a dictionary |
| 14 | +thisdict = dict(name = "John", age = 36, country = "Norway") |
| 15 | +print(thisdict) |
| 16 | +Output: |
| 17 | +{'name': 'John', 'age': 36, 'country': 'Norway'} |
| 18 | +#You can access the items of a dictionary by referring to its key name, inside square brackets |
| 19 | +thisdict = { |
| 20 | + "brand": "Ford", |
| 21 | + "model": "Mustang", |
| 22 | + "year": 1964 |
| 23 | +} |
| 24 | +x = thisdict["model"] |
| 25 | +print (x) |
| 26 | +Output: |
| 27 | +Mustang |
| 28 | +#The keys() method will return a list of all the keys in the dictionary |
| 29 | +x = thisdict.keys() |
| 30 | +print (x) |
| 31 | +Output: |
| 32 | +dict_keys(['brand', 'model', 'year']) |
| 33 | +#The values() method will return a list of all the values in the dictionary |
| 34 | +x = thisdict.values() |
| 35 | +print (x) |
| 36 | +Output: |
| 37 | +dict_values(['Ford', 'Mustang', 1964]) |
| 38 | +#Make a change in the original dictionary, and see that the values list gets updated as well |
| 39 | +car = { |
| 40 | +"brand": "Ford", |
| 41 | +"model": "Mustang", |
| 42 | +"year": 1964 |
| 43 | +} |
| 44 | + |
| 45 | +x = car.values() |
| 46 | + |
| 47 | +print(x) #before the change |
| 48 | + |
| 49 | +car["year"] = 2020 |
| 50 | + |
| 51 | +print(x) #after the change |
| 52 | + |
| 53 | +Output: |
| 54 | +dict_values(['Ford', 'Mustang', 1964]) |
| 55 | +dict_values(['Ford', 'Mustang', 2020]) |
| 56 | +#Check if “model” is present in the dictionary |
| 57 | +thisdict = { |
| 58 | + "brand": "Ford", |
| 59 | + "model": "Mustang", |
| 60 | + "year": 1964 |
| 61 | +} |
| 62 | +if "model" in thisdict: |
| 63 | + print("Yes, 'model' is one of the keys in the thisdict dictionary") |
| 64 | +Output: |
| 65 | +Yes, 'model' is one of the keys in the thisdict dictionary |
| 66 | +#The update() method will update the dictionary with the items from the given argument |
| 67 | +thisdict = { |
| 68 | + "brand": "Ford", |
| 69 | + "model": "Mustang", |
| 70 | + "year": 1964 |
| 71 | +} |
| 72 | +thisdict.update({"year": 2020}) |
| 73 | +print (thisdict) |
| 74 | +Output: |
| 75 | +{'brand': 'Ford', 'model': 'Mustang', 'year': 2020} |
| 76 | +#Adding an item to the dictionary is done by using a new index key and assigning a value to it |
| 77 | +thisdict = { |
| 78 | + "brand": "Ford", |
| 79 | + "model": "Mustang", |
| 80 | + "year": 1964 |
| 81 | +} |
| 82 | +thisdict["color"] = "red" |
| 83 | +print(thisdict) |
| 84 | +Output: |
| 85 | +{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'} |
| 86 | +#The pop() method removes the item with the specified key name: |
| 87 | +thisdict = { |
| 88 | + "brand": "Ford", |
| 89 | + "model": "Mustang", |
| 90 | + "year": 1964 |
| 91 | +} |
| 92 | +thisdict.pop("model") |
| 93 | +print(thisdict) |
| 94 | +Output: |
| 95 | +{'brand': 'Ford', 'year': 1964} |
| 96 | +#The clear() method empties the dictionary: |
| 97 | +thisdict = { |
| 98 | + "brand": "Ford", |
| 99 | + "model": "Mustang", |
| 100 | + "year": 1964 |
| 101 | +} |
| 102 | +thisdict.clear() |
| 103 | +print(thisdict) |
| 104 | +Output: |
| 105 | +{} |
| 106 | +#Make a copy of a dictionary with the copy() method: |
| 107 | +thisdict = { |
| 108 | + "brand": "Ford", |
| 109 | + "model": "Mustang", |
| 110 | + "year": 1964 |
| 111 | +} |
| 112 | +mydict = thisdict.copy() |
| 113 | +print(mydict) |
| 114 | +Output: |
| 115 | +{'brand': 'Ford', 'model': 'Mustang', 'year': 1964} |
| 116 | +#Add Items to a Python Dictionary with Different Data Types |
| 117 | +Dict = {} |
| 118 | +print("Empty Dictionary: ") |
| 119 | +print(Dict) |
| 120 | +Dict[0] = 'Go' |
| 121 | +Dict[2] = 'For' |
| 122 | +Dict[3] = 1 |
| 123 | +print("\nDictionary after adding 3 elements: ") |
| 124 | +print(Dict) |
| 125 | + |
| 126 | +Dict['Value_set'] = 2, 3, 4 |
| 127 | +print("\nDictionary after adding 3 elements: ") |
| 128 | +print(Dict) |
| 129 | + |
| 130 | +Dict[2] = 'Welcome' |
| 131 | +print("\nUpdated key value: ") |
| 132 | +print(Dict) |
| 133 | +Dict[5] = {'Nested': {'1': 'Life', '2': 'Python'}} |
| 134 | +print("\nAdding a Nested Key: ") |
| 135 | +print(Dict) |
| 136 | + |
| 137 | +Output: |
| 138 | +Empty Dictionary: |
| 139 | +{} |
| 140 | + |
| 141 | +Dictionary after adding 3 elements: |
| 142 | +{0: 'Go', 2: 'For', 3: 1} |
| 143 | + |
| 144 | +Dictionary after adding 3 elements: |
| 145 | +{0: 'Go', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)} |
| 146 | + |
| 147 | +Updated key value: |
| 148 | +{0: 'Go', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)} |
| 149 | + |
| 150 | +Adding a Nested Key: |
| 151 | +{0: 'Go', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'Python'}}} |
| 152 | +#Dictionary Methods: clear, copy, fromkeys, get, items, keys, pop, popitem, setdefault, update, and values. |
0 commit comments