Initialize a Dictionary with Custom Value list in Python


The dictionary is defined by storing the key with value pair whereas a custom value list in the dictionary means the list connected with all the key elements can be modified or changeable according to conditions. This gives an efficient approach to a program that users can add, modify, or remove within a list to require fulfillment. In Python, we have some built−in functions− range(), len(), zip(), dict(), and, enumerate() will Initialize a dictionary with custom value list.

Syntax

The following syntax is used in the examples-

range()

The built−in function range() is defined by returning the sequence of numbers according to the given length.

len()

The built−in function len() is defined by returning the length object.

zip()

The zip() is an in−built function in Python that can be used to iterate any number by accepting parameters such as list, tuple, string, etc.

dict()

The dict() is an in−built function in Python that create the dictionary. This dictionary collects the specific keys with element.

enumerate()

The built−in function enumerate() works by iterating the sequence of elements and keeping track of particular index positions.

Using for loop

The program uses built−in functions len() and range() in the for−loop iteration over the key and value list. Using this process the dictionary set the custom value list.

Example

In the following example, we will start the program by initializing two different lists− keys and values for the dictionary. Then use the empty dictionary in the variable res that will store the result in the form of a dictionary. Next, using for loop it iterates through the first list of the program and set all the elements as key. Now using the assignment operator i.e. = it will set the second list as value in dictionary. Finally, we are printing the result with the help of a variable named res.

k = ['P', 'Q', 'R', 'S', 'T']
v = [11, 92, 53, 94, 67]
res = {}
for i in range(len(k)):
    res[k[i]] = [v[i]]
print("Dictionary with custom value list in Python:\n", res)

Output

 Dictionary with custom value list in Python:
 {'P': [11], 'Q': [92], 'R': [53], 'S': [94], 'T': [67]}

Using zip() Function

The program uses two built−in functions dict() and zip() to initialize a dictionary with a custom value list.

Example

In the following example, start initializing the two variables−keys and values that will store the element in the form of list. Next, use the zip() function inside the dict() function that accepts two parameters− k(set the key) and v(the variable i iterate to set the value with a particular key). All these processes are stored in the variable res. Finally, we are printing the result with the help of variable res.

k = ['M', 'N', 'O']
v = [100, 200, 300]
res = dict(zip(k, [[i] for i in v]))
print("The Custom value in the form of a list:\n", res)

Output

 The Custom value in the form of a list:
 {'M': [100], 'N': [200], 'O': [300]}

Using Dictionary

The program uses the built−in functions len() and range() to calculate the specific length of the given list and set the range by ordering of given elements from the list.

Example

In the following example, we will start storing two lists in the variables− Subject and Score respectively. Then apply dictionary i.e. {} to calculate the particular key element with value pair using for loop and store it in the variable res. Next, display the program output with the help of variable res.

Subject = ['Math', 'English', 'Hindi', 'Science']
Score = [95, 78, 82, 65]
res = {Subject[i]: [Score[i]] for i in range(len(Subject))}
print("Subject with a score:\n", res)

Output

 Subject with a score:
 {'Math': [95], 'English': [78], 'Hindi': [82], 'Science': [65]}

Using enumerate() Function

The program uses the built−in function zip() inside the function enumerate() which keeps track of the specific iteration for initializing the dictionary with a custom value list.

Example

In the following example, begin the program initialize the two variables− person(set the name list) and age(set the age of the person). Using dictionary comprehension in the variable res will calculate the specific key with value pair of dictionary and display the result.

person = ['John', 'Mark', 'Jonathan', 'Sam', 'Karl']
age = [18, 26, 22, 24, 33]
res = {person: [age] for i, (person, age) in enumerate(zip(person, age))}
print("Person name with age:\n",res)

Output

Person name with age: 
{'John': [18], 'Mark': [26], 'Jonathan': [22], 'Sam': [24], 'Karl': [33]}

Conclusion

We discussed the different methods to solve the dictionary initialization with a custom value list. Dictionary comprehension is the best approach to iterating the object with key−value pairs based on specific conditions. A real example of a dictionary is found in any book at the end, where both the topic and page number are mentioned.

Updated on: 14-Aug-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements