Print list in Python 2

I often want to print the elements of a list. Unfortunately, the print function packs the list elements and often makes the output unreadable(e.g., Figure 1). Below are three ways to print list elements so that each element appears on a new line.

Print list in Python 2

Figure 1: Using the standalone print function to print list elements.

  1. For a beginner, the easiest way is to use a for loop to print each list element. However, this method is my least favorite.

    for result in results:
        print (result)
    
    # Element number: 1, Result: 212
    # Element number: 2, Result: 134
    # Element number: 3, Result: 151
    # Element number: 4, Result: 104
    # Element number: 5, Result: 174
    
  2. Use a starred expression to unpack the list. In the print function below, the asterisk * denotes iterable unpacking. You can read more about Expression lists in .

    print(*results,sep='\n')
    
    # Element number: 1, Result: 212
    # Element number: 2, Result: 134
    # Element number: 3, Result: 151
    # Element number: 4, Result: 104
    # Element number: 5, Result: 174
    
  3. Use the print function with the join() method. The join() method takes all items in the list and joins them into a single string.

    Arrays - sometimes known as lists - are a basic, but powerful data structure present in almost all programming languages. They make it easy to store ordered data, which then allows for fast and easy to access that data using a numeric index. In Python, arrays are referred to as lists, and they are known for being easier to use in Python compared to other programming languages.

    Efficiently printing all the data elements stored in a list can be a very useful, if not essential task for certain workflows.

    In this article, we will explore various ways to print lists in Python along with the benefits of each.

    Python print list with the >>> x = [1, 2, 3, 4] >>> print(*x) 1 2 3 4 >>> 2 function

    The simplest way of printing a list is directly with the

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    2 function:

    >>> x = [1, 2, 3, 4]
    >>> print(x)
    [1, 2, 3, 4]
    

    This method prints the raw Python list syntax (including the brackets and commas), but you can use the

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    5 symbol to print the list much more elegantly. For example:

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    

    Here, only the items in the list are printed in order with a space separating them.

    This simple method of printing lists seems to work fine, so one might wonder why not just use this method to print lists all the time. This method works well for small, simple lists like the one shown above. But if you were to use a list with larger and more complex data, it quickly becomes difficult to comprehend the output.

    For example, let's say you have a list with a bunch of lengthy strings:

    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    

    In the example above, it is hard to find where one string starts and ends, and this becomes even more difficult for bigger lists. You can print this kind of list in a more organized manner, with the

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    5 symbol and the
    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    7 argument in the print() function. The
    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    7 argument allows you to replace the default space that separates items in a print() function with any other character.

    >>> print(*longStrings, sep='\n------\n')
    first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
    ------
    second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
    ------
    third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
    >>> 
    

    Now each item in the list is separated in a way that makes the output much easier to read.

    Another way to print a list in Python is using loops.

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    9 loops are the more intuitive and simple option, but you can also use
    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    0 loops. So we will explore both ways here.

    With for loops, you can print the list by simply looping through each item and printing it:

    >>> 
    >>> x = [1, 2, 3, 4]
    >>> 
    >>> for item in x:
    ...     print(item)
    ... 
    1
    2
    3
    4
    >>> 
    

    This allows you to individually format how each item will be printed out. However, each item will always be printed on a new line. But, you can fix this with the

    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    1 argument in the print() function. This argument allows you to replace the '\n' (newline) character, which is default after every print() output element, with another string:

    >>> for item in x:
    ...     print(item, end=" - ")
    ... 
    1 - 2 - 3 - 4 - >>> 
    

    You can also print a list with a for loop by looping through the range of indices:

    >>> for idx in range(len(x)):
    ...     print(x[idx])
    ... 
    1
    2
    3
    4
    >>> 
    

    This can prove useful when you want to specify the way you print the items in the list based on their index value.

    We can use this same idea of using indices to print items and instead use a while loop.

    >>> idx = 0
    >>> while idx < len(x):
    ...     print(x[idx])
    ...     idx += 1
    ... 
    1
    2
    3
    4
    >>>
    

    First, the

    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    2 variable is set to 0, and then the while loop prints the value of
    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    3 at index
    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    2 and increments
    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    2 by 1. The loop ends when
    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    2 reaches the length of the list
    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    3.

    Another way to print lists is to use the

    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    8 method to join the items in the list into a single string, and then print it.

    >>> y = ['a', 'b', 'c', 'd']
    >>> joinedList = ' '.join(y)
    >>> print(joinedList)
    a b c d
    >>> 
    

    Here, the items in list

    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    9 are joined with space between them. You can replace the space with any string you want to use as a separator.

    However, there is one caveat to using the join() method. join() is a string method, which means that all the elements in the list must strings. If you know that the items in the list are not all strings or you are uncertain, you can use the

    >>> print(*longStrings, sep='\n------\n')
    first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
    ------
    second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
    ------
    third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
    >>> 
    
    0 function to make use of join().

    With the map() function you can map all items in the list to the

    >>> print(*longStrings, sep='\n------\n')
    first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
    ------
    second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
    ------
    third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
    >>> 
    
    1 function which will convert the items to string before they are joined:

    >>> x = [1, 2, 'a', 'b']
    >>> joinedList = ' - '.join(map(str, x))
    >>> print(joinedList)
    1 - 2 - a - b
    >>> 
    

    Here, even though the list

    >>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    >>> print(longStrings)
    ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
    >>> 
    
    3 consists of both integers and strings, the items are converted to string datatype with map() and then joined.

    The key advantage of using the join() method to print lists is that you can store the printed output to a variable beforehand. This can be useful in certain situations, where you want to do something else with the output rather than printing it.

    Another way you might want to print a list is in reverse. For this, python has the

    >>> print(*longStrings, sep='\n------\n')
    first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
    ------
    second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
    ------
    third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
    >>> 
    
    3 method and the
    >>> print(*longStrings, sep='\n------\n')
    first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
    ------
    second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
    ------
    third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
    >>> 
    
    4 function. The reverse() method will reverse the original list in-place - the original list will be modified. The reversed() function will return an iterator that will output the items in reversed order, leaving the original list unaffected.

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    0

    With the reverse() method you can reverse the original list and then use the techniques shown in the previous section to print the list accordingly. For the reversed() function you can convert the iterator output into a list with the

    >>> print(*longStrings, sep='\n------\n')
    first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
    ------
    second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
    ------
    third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
    >>> 
    
    5 function and then print the list. But, when using a for loop to print the list you can directly loop through the iterator output of reversed():

    >>> x = [1, 2, 3, 4]
    >>> print(*x)
    1 2 3 4
    >>> 
    
    1

    Reversing a list and then printing its output can be very useful in situations dealing with sorted lists.

    Summary

    In this article, we discussed various ways to print lists in Python and the benefits of each method.

    First, you saw the most basic way to print a list directly with the print() function. Then we discussed why the basic way might not be sufficient. Next, you saw how you can print lists with for loops and while loops. Then you saw how you can use the join() method to print a list and understood why it might be advantageous in certain situations. Finally, you saw how to print a list in reverse with the reversed() function and reverse() method.

    Next Steps

    If you're interested in learning more about the basics of coding, programming, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.

    Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to [email protected].

    Does print () work in Python 2?

    Print: In Python 2, “print” is treated as a statement rather than a function. There is no need to wrap the text you want to print in parentheses, although you can if you want. This can be confusing, as most other actions in Python use functions that require the arguments to be placed inside parentheses.

    How do I print an entire list in Python?

    Using the * symbol to print a list in Python. To print the contents of a list in a single line with space, * or splat operator is one way to go. It passes all of the contents of a list to a function. We can print all elements in new lines or separated by space and to do that, we use sep=”\n” or sep=”, ” respectively.

    How do you print a list without brackets in Python 2?

    Call print(*value, sep=" ") with value as a list to unpack and print the elements of the list seperated by the zero or many characters contained in sep. To seperate each element with a comma followed by a space, set sep to ", ".

    How do you print common elements that are present in 2 series?

    Method 1:Using Set's & property Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Below is the Python3 implementation of the above approach: Python3.