Python Dictionary - Tutorial Kart
Python Dictionary
Python Dictionary
Python Dictionary is a collection. It can contain multiple elements.
Python Dictionary is un-ordered. The elements in the dictionary are not stored in a sequence. For example, if you add an item to a dictionary, it can be inserted at any index.
Python Dictionary is changeable. You can update an item using its key, delete an item, or add an item. The original dictionary gets updated. Simply put, Python Dictionary is mutable.
Python Dictionary is indexed. You can access a specific key:value pair using key as index.
Create a Python Dictionary with Initial Values
To create a python dictionary, assign a variable with comma separated key:value pairs enclosed in curly braces.
In the following example, we create a dictionary with some initial key:value pairs.
#initialize tuple aDict = {
'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' }
In the above example, tallest building , longest river and biggest ocean are keys while Burj Khalifa , The Nile and The Pacific Ocean are their corresponding values.
Access Specific Key:Value Pair
To access a specific key:value pair in a dictionary, use key as index on the dictionary.
#initialize tuple aDict = {
'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' } print(aDict['longest river'])
Output
The Nile
Iterate through key:value pairs of Python Dictionary
To iterate through all key:value pairs of a Python Dictionary, you can use for loop as shown below.
#initialize tuple aDict = {
'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' }
for key in aDict: print(key,':',aDict[key])
Output
tallest building : Burj Khalifa longest river : The Nile biggest ocean : The Pacific Ocean
Add a new Key:Value Pair to the Dictionary
To add a new key value pair to the dictionary, just assign the value to the dictionary using key as index.
#initialize tuple aDict = {
'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' }
aDict['biggest forest'] = 'The Amazon'
for key in aDict: print(key, ':', aDict[key])
Output
tallest building : Burj Khalifa longest river : The Nile biggest ocean : The Pacific Ocean biggest forest : The Amazon
Dictionary Operations
You can perform many other operations on Dictionary. You can refer these following tutorials for different operations available on a Python Dictionary.
Get Keys of Python Dictionary as a List Get Values of Python Dictionary as a List Remove all elements of a Python Dictionary Get Length of Python Dictionary Compare two Python Dictionaries
Conclusion
In this Python Tutorial, we learned how to initialize a Dictionary in Python, how to iterate through the key:value pairs of the dictionary, how to add a new key:value pair to the dictionary and many other operations that could be performed on a Python Dictionary.
Python Programming
Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments
Control Statements
Python If Python If Else Python While Loop Python For Loop
Python String
Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings
Functions
Python Functions
Python Collections
Python List Python Dictionary
Advanced
Python Multithreading
Useful Resources
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- how to get keys of python dictionary as list tutorial kart
- ppyytthhoonn ddiiccttiioonnaarryy
- dictionaries store connections between pieces of list comprehensions a
- python tuple define access iterate
- cme193 introductiontoscientificpython lecture3 tuples sets
- beginners python cheat sheet pcc dictionaries anarcho copy
- python dictionary tutorial kart
- chapter 12 dictionary hash tables college of engineering
- dictionary case study gktcs
- chapter 2 lists arrays and dictionaries uc davis
Related searches
- python dictionary keys
- python dictionary key to list
- python dictionary get value
- python dictionary get value list
- sort python dictionary by values
- python dictionary append sorted list
- python dictionary of lists
- python dictionary sort by values descending
- python dictionary append
- python dictionary list of values
- python dictionary sort reverse
- python dictionary key order