Python Program to Construct dictionary using random values
Last Updated :
22 Apr, 2023
Given List, our task is to write a Python program to construct a dictionary with values randomly selected from range.
Examples:
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
from random import randint
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 9
res = dict ()
for ele in test_list:
res[ele] = randint(i, j)
print ( "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
from random import randint
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 9
res = {ele : randint(i, j) for ele in test_list}
print ( "Random range initialized dictionary : " + str (res))
|
Output:
The original list is : [‘Gfg’, ‘is’, ‘Best’]
Random range initialized dictionary : {‘Gfg’: 4, ‘is’: 2, ‘Best’: 6}
Method #3: Using zip() and map()
Step-by-step approach:
- Import the randint() function from the random module.
- Define a list of strings test_list.
- Define the range [i,j] for the random values to be generated.
- Print the original list test_list.
- Use map() and a lambda function to generate random values for each index in test_list.
- Use zip() to combine test_list with the list of random values generated in step 5 into a dictionary res.
- Print the resulting dictionary res.
Python3
from random import randint
test_list = [ "Gfg" , "is" , "Best" ]
i, j = 2 , 9
print ( "The original list is : " + str (test_list))
values = map ( lambda x: randint(i, j), range ( len (test_list)))
res = dict ( zip (test_list, values))
print ( "Random range initialized dictionary : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'Best']
Random range initialized dictionary : {'Gfg': 6, 'is': 4, 'Best': 6}
Time complexity: O(n), where n is the length of the list
Auxiliary Space: O(n), since the largest data structure used in the code is the dictionary res with n key-value pairs.
Method #4: Using randrange() function:
- Importing the randrange() function from the random module to generate random integers within a given range.
- Initializing a list of strings and printing the original list.
- Initializing the range limits to generate random values.
- Using dictionary comprehension to iterate through the list and generate a dictionary with each element as the key and a random integer between i and j as the value.
- Assigning the resulting dictionary to a variable res.
- Printing the resulting dictionary.
Python3
from random import randrange
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 9
res = {ele: randrange(i, j + 1 ) for ele in test_list}
print ( "Random range initialized dictionary : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'Best']
Random range initialized dictionary : {'Gfg': 4, 'is': 8, 'Best': 3}
Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n), since the largest data structure used in the code is the dictionary res with n key-value pairs.
Share your thoughts in the comments
Please Login to comment...