f4l f4l - 1 month ago 8
Python Question

Ho do i make one to many relations in list of tuples

I have a list consisting of tuples like the following:

m = [('a', 'b', 'c', 'd'), (1, 2, 3, 4), ('alpha', 'beta', 'gamma', 'eta')]


how can i get the following output:

['c', 3, 'gamma']


need only for 'c' and not for rest of the elements a,b,d

Answer

Variable 'm' contains list of tuples, there are 3 tuples considered as 3 items in list.

Variable 'n' takes each element (tuple) in the list and prints its value according to its index number.

m = [('a', 'b', 'c', 'd'), (1, 2, 3, 4), ('alpha', 'beta', 'gamma', 'eta')]
for n in m:
    print (n[2])
Comments