Search code examples
pythonpython-3.xlistiterator

How to iterate over multiple lists of different lengths, but repeat the last value of a shorter list until the longest list is done?


In my Python 3 script, I am trying to make a combination of three numbers from three different lists based on inputs. If the lists are the same size, there is no issue with zip. However, I want to be able to input a single number for a specific list and the script to repeat that number until the longest list is finished. This can be done with zip_longest. However, with fillvalue it is not possible to have separate fill values for separate lists.

Taking this simple script as an example:

from itertools import zip_longest

list1=[1]
list2=[4, 5, 6, 7, 8, 9]
list3=[2]
for l1, l2, l3 in zip_longest(list1, list2, list3):
     print(l1, l2, l3)

This is the actual result:

# 1    4 2
# None 5 None                                                        
# None 6 None                                                         
# None 7 None
# None 8 None
# None 9 None  

And this would be the result that I want:

# 1 4 2
# 1 5 2                                                        
# 1 6 2                                                         
# 1 7 2
# 1 8 2
# 1 9 2                                                        
 

I already managed to do this specific task by manually creating different for loops and asking if a list is a constant or not, but zip_longest is so close to exactly what I need that I wonder if I am missing something obvious.


Solution

  • You could make use of logical or operator to use the last element of the shorter lists:

    from itertools import zip_longest
    list1 = [1]
    list2 = ["a", "b", "c", "d", "e", "f"]
    list3 = [2]
    for l1, l2, l3 in zip_longest(list1, list2, list3):
        print(l1 or list1[-1], l2, l3 or list3[-1])
    

    Out:

    1 a 2
    1 b 2
    1 c 2
    1 d 2
    1 e 2
    1 f 2