pythonlistpositional-operator

Print list of lists using *(expansion operator) in single line of code


I am trying to print list lst using a single line of code

lst = [("A",23),("B",45),("C",88)]

print(*lst, sep="\n")

Output comes like this:

('A', 23)
('B', 45)
('C', 88)

What I am expecting is

A 23
B 45
C 88

However, this can be achieved by the following code

for i in range(len(lst)):
    print(*lst[i], sep=" ")

But I dont want to use a "for loop", rather to use * operator or any other technique to accomplish that in a single line of code


Solution

  • You could do it in one line like this:

    print('\n'.join('{} {}'.format(*tup) for tup in lst))