Home > Software design >  Single Liners for 'for' loops?
Single Liners for 'for' loops?

Time:10-24

anyone knows a better implementation for this.. I'm trying to not assign each element in the list to a variable, instead put it directly in the for loop. Here's a snip of code to explain better.

x, y = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(x, y):
    print (it)

What I am trying to do is use a single liner in place of (x, y) in the 2nd line. Since the number of lists in the List vary.

mainList = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(*where I want to use the single liner*):
    # The single liner should be putting passing the lists in the mainList as seperate params.
    print (it)

I think this could be implemented in other loops as well.

Note: The single liner should be putting passing the lists in the mainList as seperate params.

CodePudding user response:

You could use tuple unpacking.

mainList = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(*mainlist):
    print (it)
  • Related