Create your dictionary variable in Python with simple methods given here. You can perform various operations with Python dictionary variable. These operations contain accessing, looping over elements and other useful operations.
You can get the keys and the elements dictionary related to these keys using Python.
What is Dictionary Variable
The dictionary variable contains the collection of unordered elements. The dictionary variable contains the elements enclosed within the curly brackets({}). The list variable also enclosed within the curly brackets. However, the dictionary variable contains the keys and the elements.
The dictionary variable is changeable and indexed. It can contain a combination of elements with string or integers. You can change or remove each element of the dictionary using Python. The dictionary contains keys and each key has its related elements.
Create Dictionary Variable Using Python
To create your dictionary variable in Python, you have to use the curly brackets({}) to place the elements. Each element contains keys and its matched element. The elements are indexed and start from index zero(0).
You have to place dictionary variable in comma separation. Use the colon(:) between the keys and it’s matching element to create the dictionary variable in Python.
1 2 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} print(myDict); |
Output
The above example contains the dictionary variable with 6 elements. The output contains all the elements enclosed within the curly brackets. However, you can access only the required elements of a dictionary with the below-given method.
Access Items of Dictionary Variable in Python
The dictionary variable in Python works like an associated array in other programming languages. The elements are indexed with the key elements.
To access the elements of the dictionary variable, you have to use the index operator([]). The index operator requires one argument as the key position of the elements. Use the method as given below to get the element of the dictionary in Python.
1 2 3 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} print(myDict['one']); print(myDict['two']); |
Output
18
The above example access the first and the second element of the dictionary variable. ‘one’ and ‘two’ are the keys for the element which you can use to get the required elements.
Add Items in Dictionary Variable in Python
If you want to add new items to the dictionary using Python. You have to use a new index key and assign a new value to it. You can use any new key for adding the new element to the dictionary. Each time you create and assign new key and value, it gets appended to the end of the dictionary in Python.
1 2 3 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} myDict['seven'] = 32; print(myDict); |
Output
The above example showing the dictionary with the added elements. Previously there are six elements but after the addition, you will get seven elements in the output.
If you don’t know the size of the dictionary after addition of the new element, you can use the below example to find the length of the dictionary.
Find Length of Dictionary in Python
The size of the dictionary variable is the number of elements contained in it. You can get or find the length of the dictionary using the len()
function. Pass the dictionary variable as the argument of the len function. See the example below to get the length of your dictionary variable in Python.
1 2 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} print(len(myDict)); |
Output
The above example showing the length of the dictionary as 6. There are 6 elements and each element contains the keys with its values.
Change Items of Dictionary
In addition to adding the new element to the dictionary, you can also change the elements. To change the elements of the dictionary variable, you have to use the method given below. The method assigns the new values to the old keys to change the element values.
1 2 3 4 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} myDict['one'] = "Chinu"; myDict['five'] = "Champ"; print(myDict); |
Output
The above example showing the dictionary with the changes elements. The first and the fifth elements of the dictionary variable contains the new elements. The output showing the changed elements with the other old elements of the dictionary.
Delete Dictionary Items Using Python
In addition to adding the element to the dictionary in Python. You can also perform deletion operation with the dictionary. To delete any dictionary element, you have to use the pop()
function of Python.
The delete operation deletes the elements you want with its key and the value. You can delete only the single element at one time using pop()
function. To delete more elements of the dictionary, you have to use the pop() again and again.
1 2 3 4 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} print(myDict.pop("two")); print(myDict.pop("five")); print(myDict); |
Output
21
{‘one’: ‘Bill’, ‘three’: ‘Smith’, ‘four’: ‘Dara’, ‘six’: ‘Wilson’}
The above example showing two elements which you delete using pop(). After that, it prints the dictionary after each deletion. Before deletion, the dictionary contains 6 elements. However, after deletion, it results out 4 elements.
Perform Iteration or Loop Through Dictionary Elements.
If you want to perform iteration over the dictionary. You have to use the below-given method. There are six elements in the dictionary which gives iteration 6 times.
1 2 3 |
myDict = {'one': 'Bill', 'two': 18, 'three': 'Smith', 'four': 'Dara', 'five': 21, 'six': 'Wilson'} for d in myDict: print(myDict[d]); |
Output
18
Smith
Dara
21
Wilson
The above prints all the elements of the dictionary in the output after iteration. The elements are printed with a single element in a single line.
You may also like to read
- Create String Variable In Python With Examples
- How to Create Number Variables In Python
- Run Or Execute Python Program On Windows
Hope, you like this post of How to create Python dictionary variable. If you have any query regarding the tutorial, please comment below.
Also tell me, which method you are using to create your Python dictionary.
The use of semi-colons at the end of each statement is truly archaic and needless Python syntax.That was just a hang-over from its early ‘C’-oriented days and has no place in the modern world.