
- 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
Python Program to print key value pairs in a dictionary
A dictionary in python is a data structure which stores a collection of key value pairs. Unlike other data structures a dictionary contains 2 value at a particular place. A dictionary is a collection which is ordered, mutable and does not allow duplicate elements.
A dictionary can be created by placing a sequence of elements within curly { } brackets , separated by ‘comma’ ( , ) . Dictionary holds pairs of values, one being the key and the other corresponding pair element being its value.
Values in a dictionary can be of any data type and can be duplicated which means multiple keys can have same value, whereas keys can’t be repeated and must be unique. The name of keys in a dictionary is case sensitive.
We can declare a dictionary in the following way −
thisdict = { "first": "Rohan" , "second": "Suresh" , "third": “Raj” }
Now as we know what a dictionary is and how we can declare it we will look into method of printing the key and value pairs in python.
In this article we will look into 4 methods to print key value pairs in a dictionary in python.
Using the “in ” operator in python
The in operator determines whether a given value is a constituent element of a sequence such as a string, array, list, or tuple.
We can use this operator to iterate through the dictionary and then print the key and value for every iterator.
Example
Let us see an example for this –
dict = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'mango' } print ("Original dictionary is : " + str(dict)) print ("Dict key-value are : ") for i in dict : print( i, "-",dict[i], sep = " ")
Output
The output for the above code will be as below −
Original dictionary is : {'first': 'apple', 'second': 'orange', 'third': 'mango'} Dict key-value are : first - apple second - orange third - mango
Using the list comprehension in python
The list comprehension is a shorter way to create a new list based on the values of an existing list or tuple or dictionary.
Example
In the following example we have used the list comprehension method to print the key value pairs in a dictionary it is similar to the for loop approach but written using the list comprehension method in just 1 line.
dict = { 'first' : 'apple' , 'second' : 'orange' , 'third': 'mango' } print ("Original dictionary is : " + str(dict)) print (" Dict key-value are : ") print([ ( key , dict[key] ) for key in dict])
Output
The output for the above code will be as below −
Original dictionary is : {'first': 'apple', 'second': 'orange', 'third': 'mango'} Dict key-value are : [('first', 'apple'), ('second', 'orange'), ('third', 'mango')]
Using the dict.items () function in python
In Python Dictionary, items() method is used to return the list with all dictionary keys with values. In this section we will use the items() function to print the key and value for every iterator.
Example
In the following code we have used the in operator to iterate through the dictionary and printed the key and value at every iteration
dict = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'mango' } print ("Original dictionary is : " + str(dict)) print ("Dict key-value are : ") for key, value in dict.items(): print (key, value)
Output
The output for the above code will be as below −
Original dictionary is : { ‘first’ : ‘apple’ , ‘second’ : ‘orange’ , ‘third’ : ‘mango’ } Dict key-value are : first apple second orange third mango
Conclusion
In this article, we came to know about what dictionaries are in python where we can use dictionaries. We came to know about different ways we can access the key and value pairs in a dictionary and how we can print it. We came to know about 3 different methods to print the key value pair.
The first method was by using the in operator of python and iterating through the dictionary to access the key and value pairs and printing them simultaneously. In the second method we used the list comprehension method which offers us to write a for in loop in a single line. The third method involved the use of dict.items () function to get the key value pairs in each iteration and printing them.
The time complexity of each of the above approaches is O (n). Where n is the length of the dictionary.
- Related Articles
- How to print a value for a given key for Python dictionary?
- How to extract subset of key-value pairs from Python dictionary object?
- Accessing Key-value in a Python Dictionary
- Add a key value pair to dictionary in Python
- Python Program to Alternate list elements as key-value pairs
- Get the number of key/value pairs in the Dictionary in C#
- Get key from value in Dictionary in Python
- How to update the value of a key in a dictionary in Python?
- C++ Program to Update value of Dictionary using key
- Swift Program to Update value of Dictionary using key
- Java Program to create a HashMap and add key-value pairs
- Get key with maximum value in Dictionary in Python
- What is possible key/value delimiter in Python dictionary?
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
