
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How are dictionaries implemented in Python?
Dictionary in python is just like a map in C++ and Java. The Iike Map dictionary consists of two things: first is key and second is value. Dictionary is dynamic in nature. You can add more keys and values to the dictionary after creating the dictionary and also you can delete the key and value in the dictionary. You can add another dictionary to the currently created dictionary. Also you can add the list to the dictionary and dictionary to list.
In the dictionary you can access the elements via their respective keys.
Dictionary = { 1: "Apple", 2: "Ball", 3: "Caterpillar", 4: "Doctor", 5: "Elephant" }
Here in dictionaries 1, 2, 3 … are representing the keys and “Apple”, “Ball”, “Caterpillar”....represent the value.
Dictionary = { Key: "Value", Key : "Value", . . . . . Key : "Value" }
Access the elements
print(dictionary[1]) #print element whose key value is 1 i.e. “Apple” print(dictionary[4]) # print element whose key value is 4 “Doctor”
Insert and update elements in Dictionary
Dictionary[6] = "Flesh" # inserting element with key value 6 at last in dictionary Dictionary[3] = "Cat" # element with key value 3 is update with value “Cat”
Delete element in Dictionary
Dictionary.pop(3) # Delete element with key value 3 del Dictionary[4] # delete element with key value 4 Dictionary.popitem() # delete last inserted element in dictionary del Dictionary # this will delete whole dictionary
Built - in function for dictionary in python
Dictionary_2 = Dictionary.copy()
This copy function will copy the all values of dictionary to Dictionary_2
Dictionary.clear()
clear() function will clear the whole dictionary.
Dictionary.get(2)
get() function will return the value of key 2.
Dictionary.values()
This function will return all the values of the Dictionary.
Dictionary.update({5:”Ears”})
This function will update the value of given key
- Related Articles
- How expensive are Python dictionaries to handle?
- What are Ordered dictionaries in Python?
- How are virtual functions implemented in C++?
- How to create Ordered dictionaries in Python?
- How to merge multiple Python dictionaries?
- How do we compare two dictionaries in Python?
- How to perform Calculations with Dictionaries in Python?
- How can transfer learning be implemented in Python using Keras?
- How can we merge two Python dictionaries?
- Handling missing keys in Python dictionaries
- get() method for dictionaries in Python
- How to merge two Python dictionaries in a single expression?
- How is encapsulation implemented in C#?
- Python – Sort Dictionaries by Size
- How to find difference in keys contained in two Python dictionaries?
