Print lists in Python (4 Different Ways) Print lists in Python (4 Different Ways)

  1. Use a for loop to print

    a = [1, 2, 3, 4, 5]
    for x in range(len(a)):
     print a[x],

    So 1, 2, 3, 4, 5


  1. Print with an asterisk

    Print (*a, sep = ", ") print(*a, sep = ", ") print(*a, sep = ", ")

    The results of

    1, 2, 3, 4, 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5

  1. Convert the list to STR to print

    Print (".join(a)) print(".join(a)) print(".join(a)) Print STR (a)[1:-1] print STR (a)[1:-1] print STR (a)[1:-1] print STR (a)[1:-1]

    The results of

    Geeks for Geeks
    1, 2, 3, 4, 5

  1. Use a map to convert non-character data in the array to characters, and then print it

    # to join space a = [1, 2, 3, 4, 5] print ('. Join (map) (STR, a)) # wrap print print (' \ n '. Join (map) (STR, a))

    The results of

    One, two, three, four, five. One, two, three, four, five