Python Program to Construct dictionary using random values
Given List, our task is to write a Python program to construct dictionary with values randomly selected from range.
Examples:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Input : test_list = ["Gfg", "is", "Best"], i, j = 2, 9
Output : {'Gfg': 3, 'is': 9, 'Best': 4}
Explanation : Random values assigned between 2 and 9.
Input : test_list = ["Gfg", "is", "Best"], i, j = 2, 10
Output : {'Gfg': 3, 'is': 9, 'Best': 10}
Explanation : Random values assigned between 2 and 10.Method #1 : Using randint() + loop
In this, we iterate through each element in list and assign random number selected using randint() to construct key value pair dictionary.
Python3
# Python3 code to demonstrate working of# Construct dictionary using random values# Using randint() + loopfrom random import randint# initializing listtest_list = ["Gfg", "is", "Best"]# printing original listprint("The original list is : " + str(test_list))# initializing rangei, j = 2, 9res = dict()for ele in test_list: # assigning random elements res[ele] = randint(i, j)# printing resultprint("Random range initialized dictionary : " + str(res)) |
Output:
The original list is : [‘Gfg’, ‘is’, ‘Best’]
Random range initialized dictionary : {‘Gfg’: 5, ‘is’: 7, ‘Best’: 8}
Method #2 : Using dictionary comprehension + randint()
In this, we perform task in similar manner as above method, only difference being dictionary comprehension is used to assign dictionary in shorthand manner.
Python3
# Python3 code to demonstrate working of# Construct dictionary using random values# Using randint() + loopfrom random import randint# initializing listtest_list = ["Gfg", "is", "Best"]# printing original listprint("The original list is : " + str(test_list))# initializing rangei, j = 2, 9# assigning random elements# dictionary comprehension used as shorthandres = {ele : randint(i, j) for ele in test_list}# printing resultprint("Random range initialized dictionary : " + str(res)) |
Output:
The original list is : [‘Gfg’, ‘is’, ‘Best’]
Random range initialized dictionary : {‘Gfg’: 4, ‘is’: 2, ‘Best’: 6}


