[Python] how to pair each 2 elements of a list?

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list like this

    attach=['a','b','c','d','e','f','g','k']


    I wanna pair each two elements that followed by each other:

    lis2 = [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'k')]


    I did the following:

    Category=[]
    for i in range(len(attach)):
    if i+1< len(attach):
    Category.append(f'{attach},{attach[i+1]}')


    but then I have to remove half of rows because it also give 'b' ,'c' and so on. I thought maybe there is a better way

    Continue reading...

Compartilhe esta Página