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 code to demonstrate working of # Construct dictionary using random values # Using randint() + loop from random import randint
# initializing list test_list = [ "Gfg" , "is" , "Best" ]
# printing original list print ( "The original list is : " + str (test_list))
# initializing range i, j = 2 , 9
res = dict ()
for ele in test_list:
# assigning random elements
res[ele] = randint(i, j)
# printing result 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 code to demonstrate working of # Construct dictionary using random values # Using randint() + loop from random import randint
# initializing list test_list = [ "Gfg" , "is" , "Best" ]
# printing original list print ( "The original list is : " + str (test_list))
# initializing range i, j = 2 , 9
# assigning random elements # dictionary comprehension used as shorthand res = {ele : randint(i, j) for ele in test_list}
# printing result 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.
from random import randint
test_list = [ "Gfg" , "is" , "Best" ]
# defining the range for random values i, j = 2 , 9
# printing original list print ( "The original list is : " + str (test_list))
# using map() and lambda function to generate random values for each key values = map ( lambda x: randint(i, j), range ( len (test_list)))
# using zip() to combine keys and values into a dictionary res = dict ( zip (test_list, values))
# printing the resulting dictionary print ( "Random range initialized dictionary : " + str (res))
|
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.
from random import randrange
# initializing list test_list = [ "Gfg" , "is" , "Best" ]
# printing original list print ( "The original list is : " + str (test_list))
# initializing range i, j = 2 , 9
# using randrange() and dictionary comprehension res = {ele: randrange(i, j + 1 ) for ele in test_list}
# printing result print ( "Random range initialized dictionary : " + str (res))
|
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.