[Python] 2-d for loop iterates one letter at a time

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I've been working on a code which will flip the order of elements backwards using 2 functions, for some reason the first function which can normally print out two or more lists with spaces between each element is putting a space between each letter of the flipped list, could you tell me why it's doing so

    cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]

    def printList(nightwatch):
    for r in nightwatch:
    for c in r:
    print(c, end=' ')
    def flipOrder(listy):
    temp = []
    for r in range (len(listy)):
    for c in range (len(listy[r])):
    temp.insert (0, listy[r][c])
    listy = temp
    printList(listy)

    printList(cities)
    print ()
    print ()
    print ()
    flipOrder(cities)


    I've tried implementing the first function into the second as well as messing with the c and r loop, however this usually just causes the code to stop working, ideally i'd like to know why this is processing the list one letter at a time

    Continue reading...

Compartilhe esta Página