Create dictionary based of x items per key from two lists

rajanbond at gmail.com rajanbond at gmail.com
Mon Feb 2 13:48:00 EST 2015


On Saturday, 31 January 2015 18:39:01 UTC-8, Jason Friedman  wrote:
> > I have two lists
> >
> > l1 =  ["a","b","c","d","e","f","g","h","i","j"]
> > l2 = ["aR","bR","cR"]
> >
> > l2 will always be smaller or equal to l1
> >
> > numL1PerL2 = len(l1)/len(l2)
> >
> > I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
> >
> > So
> >
> > {
> > a:aR,
> > b:aR,
> > c:aR,
> > d:bR,
> > e:bR,
> > f:bR,
> > g:cR,
> > h:cR,
> > i:cR,
> > j:cR
> > }
> 
> Another possibility is:
> import itertools
> my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))}

NO. Sorry if this was not very clear.

In teh above example- len(l1) = 10
len(l2) = 3

So, the dict can not have more than 3 keys from l1 with same value from l2 except the last element from l1 .

So 
a,b,c will have one key- say aR
d,e,f - bR
g,h,i,j- cR- j has key cR because the number l1 is not completely divisible by l2 and leave a remainder.




More information about the Python-list mailing list