Search code examples
pythontuplessys

python sys.stdout.write a tuple 2 elements per line


Sorry the description in the title is not clear, please let me explain.

Let's say I have a tuple:

mytuple = (10, "A", 20, "B", 30, "C")

I have to print this tuple in '''sys.stdout.write'''

the result i want is something like this:

10 A
20 B
30 C

I wish to print 2 elements with a space in between on each line

I know I can alter the tuple into a nested list but that takes too much time and space. I know there must be an better way to do this bu I just can't find it, please help me, thank you very much!!


Solution

  • since you want them in pairs, you can just print when the index is divisable by 2 (assuming that there is an even number in the list I.E they are paired)

    mytuple = (10, "A", 20, "B", 30, "C")
    
    for index, item in enumerate(mytuple):
        if not index % 2:
            print(item, mytuple[index + 1])
    

    OUTPUT

    10 A
    20 B
    30 C