# Create a dictionary from a list of key and multi list of values in Python

I have a list of key:

list_date = ["MON", "TUE", "WED", "THU","FRI"]


I have many lists of values that created by codes below:

list_value = list()
for i in list(range(5, 70, 14)):
list_value.append(list(range(i, i+10, 3)))


Rules created that:

• first number is 5, a list contains 4 items has value equal x = x + 3, and so on [5, 8, 11, 14]

• the first number of the second list equal: x = 5 + 14, and value inside still as above x = x +3

[[5, 8, 11, 14], [19, 22, 25, 28], [33, 36, 39, 42], [47, 50, 53, 56], [61, 64, 67, 70]]

I expect to obtain a dict like this:

collections = {"MON":[5, 8, 11, 14], "TUE" :[19, 22, 25, 28], "WED":[33, 36, 39, 42], "THU":[47, 50, 53, 56], "FRI":[61, 64, 67, 70]}


Then, I used:

zip_iterator = zip(list_date, list_value)
collections = dict(zip_iterator)


To get my expected result.

I tried another way, like using the lambda function.

for i in list(range(5, 70, 14)):
list_value.append(list(range(i,i+10,3)))
couple_start_end[lambda x: x in list_date] = list(range(i, i + 10, 3))


And the output is:

{<function <lambda> at 0x000001BF7F0711F0>: [5, 8, 11, 14], <function <lambda> at 0x000001BF7F071310>: [19, 22, 25, 28], <function <lambda> at 0x000001BF7F071280>: [33, 36, 39, 42], <function <lambda> at 0x000001BF7F0710D0>: [47, 50, 53, 56], <function <lambda> at 0x000001BF7F0890D0>: [61, 64, 67, 70]}


I want to ask there is any better solution to create lists of values with the rules above? and create the dictionary collections without using the zip method?

One alternative approach is to take advantage of enumerate() -- which allows you to iterate over what could be thought of as a zip() of a list's indexes and values. To my eye, this version seems a bit clearer and simpler.

days = ['MON', 'TUE', 'WED', 'THU', 'FRI']

d = {
day : [
(5 + i * 14) + (j * 3)
for j in range(4)
]
for i, day in enumerate(days)
}

print(d)

• Personally, I always use itertools.product whenever there is a cartesian product involved: d = {day: (5 + i * 14) + (j * 3) for i,(j,day) in it.product(range(4), enumerate(days))}. Apr 8, 2021 at 17:17
• Just noticed the answers do not match. I retract my statement. Apr 8, 2021 at 17:19
• @Kevin: excuse me! but what is "it" in "it.product()" Apr 8, 2021 at 23:45
• @Scorpisces import itertools as it. But product() doesn't really help here.
– FMc
Apr 9, 2021 at 1:07
• Also you needn't write 3*j at all - you can instead move that to j in range(0, 12, 3) to avoid the multiplication. Apr 10, 2021 at 4:14

Another way that, as FMc's, has the advantage of not having to calculate/specify range limits (your 70 and i+10):

from itertools import count, islice

days = ['MON', 'TUE', 'WED', 'THU', 'FRI']

d = {day: list(islice(count(start, 3), 4))
for day, start in zip(days, count(5, 14))}

print(d)