Change language

Printing Lists in Python (4 Different Ways)

| |

Printing for loop : go from 0 to len (list) and print all list items one at a time using a for loop, this is standard practice.

# Python list printing program
# used for loop

a = [ 1 , 2 , 3 , 4 , 5 ]

  
# print the list using a loop

for x in range ( len (a)):

  print a [x],

Output:

 1 2 3 4 5 
  • Without using loops : * character is used to print list items on one line with a space. To print all elements on new lines or separated by spaces, use sep = ”/ n” or sep = ”,” respectively.

    # Python program for printing a list
    # without using a loop

     

    a = [ 1 , 2 , 3 , 4 , 5 ]

     
    # print the list using the * delimited operator
    # by space

    print ( * a)

     
    # print the list using * and sep statement

    print ( "printing lists separated by commas " )

      

    print ( * a, sep = ","

     
    # print on a new line

    print ( "printing lists in new line" )

      

    print ( * a, sep = "" )

    Output:

     1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 5 
  • Convert the list to a string for display : if it is a list of strings, we can simply concatenate them using the function isalnum-isspace-join /> join () , but if the list contains integers, convert it to a string and then use the join () function to join them to the string and print line.

    # Python programs and to print the list
    # by converting the list to
    # string to display

    a = [ " Geeks " , " for " , "Geeks" ]

     
    # print the list using the join () function

    print ( ’ ’ . join (a))

     
    # print the list by converting the list
    # integers to a string

    a = [ 1 , 2 , 3 , 4 , 5 ]

     

    print str (a) [ 1 : - 1

    Output:

     Geeks for Geeks 1, 2, 3, 4, 5 
  • Map Usage: use map () to convert each item in the list to a string if the list is not a string, then concatenate them:

    # Python list printing program
    # print the list by converting the list
    # integers to string using the map

      

    a = [ 1 , 2 , 3 , 4 , 5 ]

    print ( ’’ . join ( map ( str , a))) 

      

    print "in new line"

    print ( ’’ . join ( map ( str , a )))

    Output:

     1 2 3 4 5 in new line 1 2 3 4 5 
  • Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers

    News


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically