Dictionary in Python

June 21, 2017
Categorised in: Python
- Dictionary in Python is represented with key value pairs.
- Keys must be unique whereas values are not.
- Let’s understand some basics by example:
>>> a_dict = {} >>> a_dict = { "abc" : "String 1" } >>> a_dict {'abc': 'String 1'} >>> a_dict["abc"] = "String Change" >>> a_dict {'abc': 'String Change'} >>> a_dict["abc"] 'String Change' >>> a_dict[0] = "New dictionary item where key is integer" >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer'} >>> a_list = [1,2,3] >>> a_dict['some_list'] = a_list >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3]} >>> # above was putting a list inside dictionary ... >>> # now putting a dictionary inside dictionary ... >>> a_dict['dict_replica'] = a_dict >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}} >>> a_dict["dict_replica"] {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}} >>> # Hence, proving dictionaries can store all sorts of things. Cool and Weird. ... >>> len(a_dict) 4 >>> str(a_dict) "{'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}}" >>> type(a_dict) <class 'dict'> >>> temp_dict = {"key": "value"} >>> temp_dict {'key': 'value'} >>> temp_dict.clear() >>> temp_dict {} >>> del temp_dict >>> temp_dict Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'temp_dict' is not defined >>> a_dict.copy() {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}}} >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}} >>> a_dict.items() dict_items([('abc', 'String Change'), (0, 'New dictionary item where key is integer'), ('some_list', [1, 2, 3]), ('dict_replica', {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}})]) >>> a_dict.keys() dict_keys(['abc', 0, 'some_list', 'dict_replica']) >>> temp_dict = {"key": "value"} >>> a_dict.update(temp_dict) >>> a_dict {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}, 'key': 'value'} >>> a_dict.values() dict_values(['String Change', 'New dictionary item where key is integer', [1, 2, 3], {'abc': 'String Change', 0: 'New dictionary item where key is integer', 'some_list': [1, 2, 3], 'dict_replica': {...}, 'key': 'value'}, 'value']) >>>
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.