-2

Lets say i have a list of list containing:

[[1,'A'],[2,'B'],[3,'C']]

and i want to print it out as such:

1 A
2 B
3 C

I was thinking of using a for loop to print each element in the list of list but I wasn't quite sure of it.

lst = [[1,'A'],[2,'B'],[3,'C']]
for i in lst:
    print(lst[i])    #perhaps needing to use \n in the loop?
Electric
  • 517
  • 11
  • 25

2 Answers2

1

You can unpack each nested list like this:

for num, let in lst:
    print(num, let)

What you're currently doing, with for i in lst, is printing each element in lst, while you seem to think the for i in lst syntax, accompanying the lst[i] indexing, is, well, indexing the list. That's not how that works.

If you wanted your desired output while iterating in this fashion, try this:

for i in range(len(lst)):
    print(' '.join(lst[i]))

More simply, you could do this:

for i in lst:
    print(' '.join(i))
blacksite
  • 12,086
  • 10
  • 64
  • 109
0

try this:

for i in lst:
   print(i[0], i[1])
Ajax1234
  • 69,937
  • 8
  • 61
  • 102