Search code examples
pythonloopsfor-looppycharma-star

how to iterate ONLY the first tuple in a list of tuples?


I don't know if there is a way to find the inside of a tuple of ONLY the first tuple in a list?

list = [(a, b, c, d), (d, e, f, g), (h, i, j, k)]

output:
a
b
c
d

This is what my current loop looks like:

for x in list[0]:

EDIT: edited full question.


Solution

  • Input:

    _list = [('a', 'b', 'c', 'd'), ('d', 'e', 'f', 'g'), ('h', 'i', 'j', 'k')]
    
    for i in _list[0]:
        print(i)
    

    Output:

    a
    b
    c
    d
    

    EDIT:

    Maybe you could try the next() function in the standard library. Create an iterator from your list of tuples:

    iter_list = iter(_list)
    

    Then just pass it to the next() function:

    In: next(iter_list)
    Out: ('a', 'b', 'c', 'd')
    
    In: next(iter_list)
    Out: ('d', 'e', 'f', 'g')
    
    In: next(iter_list)
    Out: ('h', 'i', 'j', 'k')