
- 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
Accessing Key-value in a Python Dictionary
While analyzing data using Python data structures we will eventually come across the need for accessing key and value in a dictionary. There are various ways to do it in this article we will see some of the ways.
With for loop
Using a for loop we can access both the key and value at each of the index position in dictionary as soon in the below program.
Example
dictA = {1:'Mon',2:'Tue',3:'Wed',4:'Thu',5:'Fri'} #Given dictionary print("Given Dictionary: ",dictA) # Print all keys and values print("Keys and Values: ") for i in dictA : print(i, dictA[i])
Output
Running the above code gives us the following result −
Given Dictionary: {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri'} Keys and Values: 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri
With list comprehension
In this approach we consider the key similar to an index in a list. So in the print statement we represent the keys and the values as a pair along with the for loop.
Example
dictA = {1:'Mon',2:'Tue',3:'Wed',4:'Thu',5:'Fri'} #Given dictionary print("Given Dictionary: ",dictA) # Print all keys and values print("Keys and Values: ") print([(k, dictA[k]) for k in dictA])
Output
Running the above code gives us the following result −
Given Dictionary: {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri'} Keys and Values: [(1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri')]
With dict.items
The dictionary class has a method named items. We can access the items method and iterate over it getting each pair of key and value.
Example
dictA = {1:'Mon',2:'Tue',3:'Wed',4:'Thu',5:'Fri'} #Given dictionary print("Given Dictionary: ",dictA) # Print all keys and values print("Keys and Values: ") for key, value in dictA.items(): print (key, value)
Output
Running the above code gives us the following result −
Given Dictionary: {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri'} Keys and Values: 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri
- Related Articles
- Add a key value pair to dictionary in Python
- Get key from value in Dictionary in Python
- Accessing Values of Dictionary in Python
- Python Program to print key value pairs in a dictionary
- Get key with maximum value in Dictionary in Python
- What is possible key/value delimiter in Python dictionary?
- How to update the value of a key in a dictionary in Python?
- How to print a value for a given key for Python dictionary?
- Python – Extract Key’s Value, if Key Present in List and Dictionary
- How to get a value for a given key from a Python dictionary?
- Add key-value pair in C# Dictionary
- Finding The Biggest Key In A Python Dictionary?
- In the Python dictionary, can one key hold more than one value?
- Accessing index and value in a Python list
- How to extract subset of key-value pairs from Python dictionary object?
