-1

I have now two lists

a = ['Redemption', 'The', 'II', 'Dark']
b = [912, 813, 230, 567]

I would like to have a result of,

c = 
Redemption 912  
The 813
II 230
Dark 567

The code below is what I have tried but the output is incorrect:

aa = [a[i] for i in range(10)]
bb = [str(b[j]) for j in range(10)]

for i in aa:
    for j in bb:
        c = i + ' ' + j
    print(c)

Result is

Redemption 567
The 567
II 567
Dark 567
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
goxywood
  • 121
  • 5

1 Answers1

0

You only need a single for loop if you are matching elements on the same index, assuming the lists are the same length.

Try this:

a = ['Redemption', 'The', 'II', 'Dark']
b = [912, 813, 230, 567]

for i in range(len(a)):
    print(a[i],b[i])
Stephen
  • 69
  • 5
  • 2
    This works, but it is uglier and slower than the pythonic solution of using `zip()`. – Steven Rumbalski Sep 24 '19 at 14:36
  • Thanks! A further question, how could the output be sorted in descending order by the b list? ie, Redemption 912 The 813 Dark 567 II 230 – goxywood Sep 24 '19 at 14:46
  • @goxywood You can use zip() and sorted() to do that all in one line: c = sorted(zip(a, b), key=lambda z: z[1], reverse=True) – Stephen Sep 24 '19 at 15:02