python pretty print list of tuples

Solutions on MaxInterview for python pretty print list of tuples by the best coders in the world

showing results for - "python pretty print list of tuples"
Aurélie
22 Oct 2020
1# assuming my_list a Python list of tuples, 
2# each tuple having 2 elements
3for tuple_ in my_list:
4    print(tuple_[0], tuple_[1])
5    
6# alternatively
7for elem_1, elem_2 in my_list:
8  print(elem_1, elem_2)
9  
10# for wider list use the * operator:
11# my_list is assumed to be a list of tuple, each tuple with N elements
12for a, *b in my_list:
13  # now a is the first element of each tuple
14  # while b is a list containing all other N-1 elements of the tuple
15  print(a, b) 
16  
17# combinations are also allowed:
18for a, *b, c in my_list:
19  # a is the first element
20  # b is a list of elements from the second to the previous-to-last one
21  # c is the last element
22  print(a, b, c)
23  
24# and so on...