How to Create a Dictionary in PythonIn Python, a dictionary is an unordered sequence of data entries that can be used to record data entries in the same way that a map can. Unlike alternative data structures that only carry a singular item as an object, Dictionary contains a key: value pair. The dictionary includes a Key-Value field to improve the efficiency of this data type. Creating a DictionaryWe can create a dictionary in Python by wrapping a collection of objects in curly brackets and segregating them with a comma. The Dictionary keeps track of a couple of entries, one part of this is called a key, and the other is called the value of the key and is referred to as the key: value pair. We can store any data type as a value for the key and give the same value to two keys, but keys should be immutable and unique. Code Output: Dictionary created using curly braces: {1: 'Javatpoint', 2: 'Python', 3: 'Dictionary'} Dictionary with keys of multiple data type: {'Website': 'Javatpoint', 3: [2, 3, 5, 'Dictionary']} The built-in method dict() could also be used to generate a dictionary. By simply placing two curly brackets {}, a blank dictionary can be built. Code Output: An empty Dictionary: {} Dictionary created by using dict() method: {1: 'Python', 2: 'Javatpoint', 3: 'Dictionary'} Dictionary with key:value pair format: {1: 'Javatpoint', 2: 'Python', 3: 'Dictionary'} Adding Elements to a DictionaryThe insertion of items in the Python Dictionary could be done in various methods. Items can be added to the dictionary using this particular format Dictionary[Key] = 'Value'. We can use the in-built update() function to update a current key in a Dictionary. Nested key: values can also be inserted into a preexisting Dictionary. If the key-value combination is present in the dictionary, the value for that key is modified; if this is not the case, a new key is created and added to the Dictionary with the given value. Code Output: The empty Dictionary: {} Dictionary after addition of these elements: {0: 'Javatpoint', 2: 'Python', 3: 'Dictionary'} Dictionary after addition of the list: {0: 'Javatpoint', 2: 'Python', 3: 'Dictionary', 'list_values': (3, 4, 6)} Updated dictionary: {1: 'Javatpoint', 2: 'Python'} After addtion of a Nested Key: {0: 'Javatpoint', 2: 'Tutorial', 3: 'Dictionary', 'list_values': (3, 4, 6), 5: {'Nested_key': {1: 'Nested', 2: 'Key'}}} Removing Elements from DictionaryUsing the pop() function, we can eliminate a specific item from a dictionary. This function returns the value of the key removed. The popitem() function removes and returns any (key, value) element pair from the given dictionary. We can use the clear() function to erase all objects in one go. We could also use the del keyword to eliminate single items or even the entire dictionary. Code Output: After removing a key using pop(): {1: 'a', 2: 'b', 3: 'c', 5: 'e'} After removing an arbitrary key: {1: 'a', 2: 'b', 3: 'c'} After removing all the items: {} No dictionary of the given name Python Dictionary MethodsThe techniques that we can use with a dictionary are listed below. Some of these have already been mentioned in this tutorial.
|
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India