Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse loop inside a loop with same list

num = list(str(1234567))

for n1 in num:
    print(n1)
    for n2 in reversed(num):
        print('\t', n2)

On each iteration, it prints the first digit from the first loop and all 7 from the reverse loop. How can I print not all digits but only the last (i.e first) digit from reverse loop?

Thanks

like image 298
eozzy Avatar asked Sep 03 '26 23:09

eozzy


1 Answers

Simplest way is to just zip the forward and reverse lists together:

for n1, n2 in zip(num, reversed(num)):
    print(n1, '\t', n2)
like image 119
wich Avatar answered Sep 05 '26 15:09

wich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!