Create a dictionary by zipping together two lists of uneven length [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












24
















This question already has an answer here:



  • How to zip two differently sized lists?

    6 answers



I have two lists different lengths, L1 and L2. L1 is longer than L2. I would like to get a dictionary with members of L1 as keys and members of L2 as values.



As soon as all the members of L2 are used up. I would like to start over and begin again with L2[0].



L1 = ['A', 'B', 'C', 'D', 'E'] 
L2 = ['1', '2', '3']
D = dict(zip(L1, L2))
print(D)


As expected, the output is this:



'A': '1', 'B': '2', 'C': '3'


What I would like to achieve is the following:



'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'









share|improve this question















marked as duplicate by Roddy of the Frozen Peas, Andrew Savinykh, Azat Ibrakov, coldspeed list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 9 at 10:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Curious as to the reason for the close votes. What about this question is too broad? If it is because the OP hasn't offered any solution, then I can understand OP may not have the first clue where to begin. Based on their explanation of the question, it does not seem like they could have googled for "cycle", "circular", or other similar keywords that would have led to a solution.

    – coldspeed
    Jan 9 at 9:14











  • Nope, that's a dupe.

    – coldspeed
    Jan 9 at 10:23















24
















This question already has an answer here:



  • How to zip two differently sized lists?

    6 answers



I have two lists different lengths, L1 and L2. L1 is longer than L2. I would like to get a dictionary with members of L1 as keys and members of L2 as values.



As soon as all the members of L2 are used up. I would like to start over and begin again with L2[0].



L1 = ['A', 'B', 'C', 'D', 'E'] 
L2 = ['1', '2', '3']
D = dict(zip(L1, L2))
print(D)


As expected, the output is this:



'A': '1', 'B': '2', 'C': '3'


What I would like to achieve is the following:



'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'









share|improve this question















marked as duplicate by Roddy of the Frozen Peas, Andrew Savinykh, Azat Ibrakov, coldspeed list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 9 at 10:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Curious as to the reason for the close votes. What about this question is too broad? If it is because the OP hasn't offered any solution, then I can understand OP may not have the first clue where to begin. Based on their explanation of the question, it does not seem like they could have googled for "cycle", "circular", or other similar keywords that would have led to a solution.

    – coldspeed
    Jan 9 at 9:14











  • Nope, that's a dupe.

    – coldspeed
    Jan 9 at 10:23













24












24








24


0







This question already has an answer here:



  • How to zip two differently sized lists?

    6 answers



I have two lists different lengths, L1 and L2. L1 is longer than L2. I would like to get a dictionary with members of L1 as keys and members of L2 as values.



As soon as all the members of L2 are used up. I would like to start over and begin again with L2[0].



L1 = ['A', 'B', 'C', 'D', 'E'] 
L2 = ['1', '2', '3']
D = dict(zip(L1, L2))
print(D)


As expected, the output is this:



'A': '1', 'B': '2', 'C': '3'


What I would like to achieve is the following:



'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'









share|improve this question

















This question already has an answer here:



  • How to zip two differently sized lists?

    6 answers



I have two lists different lengths, L1 and L2. L1 is longer than L2. I would like to get a dictionary with members of L1 as keys and members of L2 as values.



As soon as all the members of L2 are used up. I would like to start over and begin again with L2[0].



L1 = ['A', 'B', 'C', 'D', 'E'] 
L2 = ['1', '2', '3']
D = dict(zip(L1, L2))
print(D)


As expected, the output is this:



'A': '1', 'B': '2', 'C': '3'


What I would like to achieve is the following:



'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'




This question already has an answer here:



  • How to zip two differently sized lists?

    6 answers







python list dictionary zip






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 20:23









coldspeed

126k23127214




126k23127214










asked Jan 8 at 16:20









MatMat

1497




1497




marked as duplicate by Roddy of the Frozen Peas, Andrew Savinykh, Azat Ibrakov, coldspeed list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 9 at 10:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Roddy of the Frozen Peas, Andrew Savinykh, Azat Ibrakov, coldspeed list
Users with the  list badge can single-handedly close list questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 9 at 10:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2





    Curious as to the reason for the close votes. What about this question is too broad? If it is because the OP hasn't offered any solution, then I can understand OP may not have the first clue where to begin. Based on their explanation of the question, it does not seem like they could have googled for "cycle", "circular", or other similar keywords that would have led to a solution.

    – coldspeed
    Jan 9 at 9:14











  • Nope, that's a dupe.

    – coldspeed
    Jan 9 at 10:23












  • 2





    Curious as to the reason for the close votes. What about this question is too broad? If it is because the OP hasn't offered any solution, then I can understand OP may not have the first clue where to begin. Based on their explanation of the question, it does not seem like they could have googled for "cycle", "circular", or other similar keywords that would have led to a solution.

    – coldspeed
    Jan 9 at 9:14











  • Nope, that's a dupe.

    – coldspeed
    Jan 9 at 10:23







2




2





Curious as to the reason for the close votes. What about this question is too broad? If it is because the OP hasn't offered any solution, then I can understand OP may not have the first clue where to begin. Based on their explanation of the question, it does not seem like they could have googled for "cycle", "circular", or other similar keywords that would have led to a solution.

– coldspeed
Jan 9 at 9:14





Curious as to the reason for the close votes. What about this question is too broad? If it is because the OP hasn't offered any solution, then I can understand OP may not have the first clue where to begin. Based on their explanation of the question, it does not seem like they could have googled for "cycle", "circular", or other similar keywords that would have led to a solution.

– coldspeed
Jan 9 at 9:14













Nope, that's a dupe.

– coldspeed
Jan 9 at 10:23





Nope, that's a dupe.

– coldspeed
Jan 9 at 10:23












5 Answers
5






active

oldest

votes


















31














Use itertools.cycle to cycle around to the beginning of L2:



from itertools import cycle
dict(zip(L1, cycle(L2)))
# 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'



In your case, concatenating L2 with itself also works.



# dict(zip(L1, L2 * 2))
dict(zip(L1, L2 + L2))
# 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





share|improve this answer
































    23














    Use itertools.cycle:



    from itertools import cycle

    L1 = ['A', 'B', 'C', 'D', 'E']
    L2 = ['1', '2', '3']

    result = dict(zip(L1, cycle(L2)))

    print(result)


    Output



    'E': '2', 'B': '2', 'A': '1', 'D': '1', 'C': '3'


    As an alternative you could use enumerate and index L2 modulo the length of L2:



    result = v: L2[i % len(L2)] for i, v in enumerate(L1)
    print(result)





    share|improve this answer






























      14














      cycle is fine, but I shall add this modulo based approach:



      L1[i]: L2[i % len(L2)] for i in range(len(L1))]





      share|improve this answer






























        7














        You can also use a collections.deque() to create an circular FIFO queue:



        from collections import deque

        L1 = ['A', 'B', 'C', 'D', 'E']
        L2 = deque(['1', '2', '3'])

        result =
        for letter in L1:
        number = L2.popleft()
        result[letter] = number
        L2.append(number)

        print(result)
        # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


        Which pops the left most item currently in L2 and appends it to the end once the number is added to the dictionary.



        Note: Both collections.deque.popleft() and collections.deque.append() are O(1) operations, so the above is still O(N), since you need to traverse all the elements in L1.






        share|improve this answer
































          5














          Other option without dependencies with good old for loop:



          D = 
          for i, e in enumerate(L1):
          D[e] = L2[i%len(L2)]

          D #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


          Or just:



           e: L2[i%len(L2)] for i, e in enumerate(L1) 
          #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





          share|improve this answer























          • Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

            – coldspeed
            Jan 9 at 9:53












          • plz help me to solve stackoverflow.com/questions/54264957/…

            – mohammed wazeem
            Jan 19 at 8:18

















          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          31














          Use itertools.cycle to cycle around to the beginning of L2:



          from itertools import cycle
          dict(zip(L1, cycle(L2)))
          # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'



          In your case, concatenating L2 with itself also works.



          # dict(zip(L1, L2 * 2))
          dict(zip(L1, L2 + L2))
          # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





          share|improve this answer





























            31














            Use itertools.cycle to cycle around to the beginning of L2:



            from itertools import cycle
            dict(zip(L1, cycle(L2)))
            # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'



            In your case, concatenating L2 with itself also works.



            # dict(zip(L1, L2 * 2))
            dict(zip(L1, L2 + L2))
            # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





            share|improve this answer



























              31












              31








              31







              Use itertools.cycle to cycle around to the beginning of L2:



              from itertools import cycle
              dict(zip(L1, cycle(L2)))
              # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'



              In your case, concatenating L2 with itself also works.



              # dict(zip(L1, L2 * 2))
              dict(zip(L1, L2 + L2))
              # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





              share|improve this answer















              Use itertools.cycle to cycle around to the beginning of L2:



              from itertools import cycle
              dict(zip(L1, cycle(L2)))
              # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'



              In your case, concatenating L2 with itself also works.



              # dict(zip(L1, L2 * 2))
              dict(zip(L1, L2 + L2))
              # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 8 at 21:20

























              answered Jan 8 at 16:22









              coldspeedcoldspeed

              126k23127214




              126k23127214























                  23














                  Use itertools.cycle:



                  from itertools import cycle

                  L1 = ['A', 'B', 'C', 'D', 'E']
                  L2 = ['1', '2', '3']

                  result = dict(zip(L1, cycle(L2)))

                  print(result)


                  Output



                  'E': '2', 'B': '2', 'A': '1', 'D': '1', 'C': '3'


                  As an alternative you could use enumerate and index L2 modulo the length of L2:



                  result = v: L2[i % len(L2)] for i, v in enumerate(L1)
                  print(result)





                  share|improve this answer



























                    23














                    Use itertools.cycle:



                    from itertools import cycle

                    L1 = ['A', 'B', 'C', 'D', 'E']
                    L2 = ['1', '2', '3']

                    result = dict(zip(L1, cycle(L2)))

                    print(result)


                    Output



                    'E': '2', 'B': '2', 'A': '1', 'D': '1', 'C': '3'


                    As an alternative you could use enumerate and index L2 modulo the length of L2:



                    result = v: L2[i % len(L2)] for i, v in enumerate(L1)
                    print(result)





                    share|improve this answer

























                      23












                      23








                      23







                      Use itertools.cycle:



                      from itertools import cycle

                      L1 = ['A', 'B', 'C', 'D', 'E']
                      L2 = ['1', '2', '3']

                      result = dict(zip(L1, cycle(L2)))

                      print(result)


                      Output



                      'E': '2', 'B': '2', 'A': '1', 'D': '1', 'C': '3'


                      As an alternative you could use enumerate and index L2 modulo the length of L2:



                      result = v: L2[i % len(L2)] for i, v in enumerate(L1)
                      print(result)





                      share|improve this answer













                      Use itertools.cycle:



                      from itertools import cycle

                      L1 = ['A', 'B', 'C', 'D', 'E']
                      L2 = ['1', '2', '3']

                      result = dict(zip(L1, cycle(L2)))

                      print(result)


                      Output



                      'E': '2', 'B': '2', 'A': '1', 'D': '1', 'C': '3'


                      As an alternative you could use enumerate and index L2 modulo the length of L2:



                      result = v: L2[i % len(L2)] for i, v in enumerate(L1)
                      print(result)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 8 at 16:22









                      Daniel MesejoDaniel Mesejo

                      16.4k21430




                      16.4k21430





















                          14














                          cycle is fine, but I shall add this modulo based approach:



                          L1[i]: L2[i % len(L2)] for i in range(len(L1))]





                          share|improve this answer



























                            14














                            cycle is fine, but I shall add this modulo based approach:



                            L1[i]: L2[i % len(L2)] for i in range(len(L1))]





                            share|improve this answer

























                              14












                              14








                              14







                              cycle is fine, but I shall add this modulo based approach:



                              L1[i]: L2[i % len(L2)] for i in range(len(L1))]





                              share|improve this answer













                              cycle is fine, but I shall add this modulo based approach:



                              L1[i]: L2[i % len(L2)] for i in range(len(L1))]






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 8 at 16:29









                              schwobasegglschwobaseggl

                              37.1k32442




                              37.1k32442





















                                  7














                                  You can also use a collections.deque() to create an circular FIFO queue:



                                  from collections import deque

                                  L1 = ['A', 'B', 'C', 'D', 'E']
                                  L2 = deque(['1', '2', '3'])

                                  result =
                                  for letter in L1:
                                  number = L2.popleft()
                                  result[letter] = number
                                  L2.append(number)

                                  print(result)
                                  # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                  Which pops the left most item currently in L2 and appends it to the end once the number is added to the dictionary.



                                  Note: Both collections.deque.popleft() and collections.deque.append() are O(1) operations, so the above is still O(N), since you need to traverse all the elements in L1.






                                  share|improve this answer





























                                    7














                                    You can also use a collections.deque() to create an circular FIFO queue:



                                    from collections import deque

                                    L1 = ['A', 'B', 'C', 'D', 'E']
                                    L2 = deque(['1', '2', '3'])

                                    result =
                                    for letter in L1:
                                    number = L2.popleft()
                                    result[letter] = number
                                    L2.append(number)

                                    print(result)
                                    # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                    Which pops the left most item currently in L2 and appends it to the end once the number is added to the dictionary.



                                    Note: Both collections.deque.popleft() and collections.deque.append() are O(1) operations, so the above is still O(N), since you need to traverse all the elements in L1.






                                    share|improve this answer



























                                      7












                                      7








                                      7







                                      You can also use a collections.deque() to create an circular FIFO queue:



                                      from collections import deque

                                      L1 = ['A', 'B', 'C', 'D', 'E']
                                      L2 = deque(['1', '2', '3'])

                                      result =
                                      for letter in L1:
                                      number = L2.popleft()
                                      result[letter] = number
                                      L2.append(number)

                                      print(result)
                                      # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                      Which pops the left most item currently in L2 and appends it to the end once the number is added to the dictionary.



                                      Note: Both collections.deque.popleft() and collections.deque.append() are O(1) operations, so the above is still O(N), since you need to traverse all the elements in L1.






                                      share|improve this answer















                                      You can also use a collections.deque() to create an circular FIFO queue:



                                      from collections import deque

                                      L1 = ['A', 'B', 'C', 'D', 'E']
                                      L2 = deque(['1', '2', '3'])

                                      result =
                                      for letter in L1:
                                      number = L2.popleft()
                                      result[letter] = number
                                      L2.append(number)

                                      print(result)
                                      # 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                      Which pops the left most item currently in L2 and appends it to the end once the number is added to the dictionary.



                                      Note: Both collections.deque.popleft() and collections.deque.append() are O(1) operations, so the above is still O(N), since you need to traverse all the elements in L1.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 8 at 17:05

























                                      answered Jan 8 at 16:35









                                      RoadRunnerRoadRunner

                                      11.2k31340




                                      11.2k31340





















                                          5














                                          Other option without dependencies with good old for loop:



                                          D = 
                                          for i, e in enumerate(L1):
                                          D[e] = L2[i%len(L2)]

                                          D #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                          Or just:



                                           e: L2[i%len(L2)] for i, e in enumerate(L1) 
                                          #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





                                          share|improve this answer























                                          • Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

                                            – coldspeed
                                            Jan 9 at 9:53












                                          • plz help me to solve stackoverflow.com/questions/54264957/…

                                            – mohammed wazeem
                                            Jan 19 at 8:18















                                          5














                                          Other option without dependencies with good old for loop:



                                          D = 
                                          for i, e in enumerate(L1):
                                          D[e] = L2[i%len(L2)]

                                          D #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                          Or just:



                                           e: L2[i%len(L2)] for i, e in enumerate(L1) 
                                          #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





                                          share|improve this answer























                                          • Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

                                            – coldspeed
                                            Jan 9 at 9:53












                                          • plz help me to solve stackoverflow.com/questions/54264957/…

                                            – mohammed wazeem
                                            Jan 19 at 8:18













                                          5












                                          5








                                          5







                                          Other option without dependencies with good old for loop:



                                          D = 
                                          for i, e in enumerate(L1):
                                          D[e] = L2[i%len(L2)]

                                          D #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                          Or just:



                                           e: L2[i%len(L2)] for i, e in enumerate(L1) 
                                          #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'





                                          share|improve this answer













                                          Other option without dependencies with good old for loop:



                                          D = 
                                          for i, e in enumerate(L1):
                                          D[e] = L2[i%len(L2)]

                                          D #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'


                                          Or just:



                                           e: L2[i%len(L2)] for i, e in enumerate(L1) 
                                          #=> 'A': '1', 'B': '2', 'C': '3', 'D': '1', 'E': '2'






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jan 9 at 9:05









                                          iGianiGian

                                          3,7982622




                                          3,7982622












                                          • Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

                                            – coldspeed
                                            Jan 9 at 9:53












                                          • plz help me to solve stackoverflow.com/questions/54264957/…

                                            – mohammed wazeem
                                            Jan 19 at 8:18

















                                          • Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

                                            – coldspeed
                                            Jan 9 at 9:53












                                          • plz help me to solve stackoverflow.com/questions/54264957/…

                                            – mohammed wazeem
                                            Jan 19 at 8:18
















                                          Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

                                          – coldspeed
                                          Jan 9 at 9:53






                                          Isn't that already here? stackoverflow.com/a/54095907/4909087 and also in another answer.

                                          – coldspeed
                                          Jan 9 at 9:53














                                          plz help me to solve stackoverflow.com/questions/54264957/…

                                          – mohammed wazeem
                                          Jan 19 at 8:18





                                          plz help me to solve stackoverflow.com/questions/54264957/…

                                          – mohammed wazeem
                                          Jan 19 at 8:18


                                          Popular posts from this blog

                                          How to check contact read email or not when send email to Individual?

                                          Displaying single band from multi-band raster using QGIS

                                          How many registers does an x86_64 CPU actually have?