In this tutorial, we will look at how to print elements of a list in Python with the help of some examples. Show
How to Print List Elements?There are multiple ways to print elements of a list in Python. For example, you can use a loop to iterate through and print the elements, you can use the 1 2 3 4 54 operator to unpack the elements in the list and directly print them, you can use the string 1 2 3 4 55 function to print the list elements as a single string, etc. Let’s look at these methods with the help of examples. Using Loop to Print List ElementsThis is a straightforward approach. Iterate through and print the list elements one by one using a loop. Let’s look at an example. # create a list ls = [1,2,3,4,5] # iterate over list elements and print them for item in ls: print(item) Output: 1 2 3 4 5 Here we use a for loop to iterate over the list 1 2 3 4 56 and print each element. Since we are explicitly iterating over each element, we can use this method for more complex list printing tasks. For example, using a specific format for each element, or only printing elements that satisfy a conditional statement. Let’s print only the odd elements in a list of numbers. # create a list ls = [1,2,3,4,5] # iterate over list elements for item in ls: # print odd elements if item % 2 != 0: print(item) Output: 1 3 5 Here we use a condition to check whether the current list element is odd or not, the element is only printed if it is odd. Let’s look at another example – Print elements in a list of real numbers with only two digits after the decimal. # create a list ls = [1.456, 2.111, 3.605] # iterate over list elements for item in ls: # print with formatting print("{:.2f}".format(item)) Output: 1.46 2.11 3.60 Here, we format the element being printed such that it prints the element with only two digits after the decimal using a format string. You can read more about format strings . Using 1 2 3 4 54 operator to unpack the listYou can also use the 1 2 3 4 54 operator to print the list elements. The 1 2 3 4 54 operator, when used before an iterable (for example, list, tuple, etc.) unpacks the elements of the iterable. # create a list ls = [1,2,3,4,5] # use * to unpack list items print(*ls) Output: 1 2 3 4 5 The list elements are printed above. You can also specify the separator you want to use when printing the elements. Pass the separator you want to the # create a list ls = [1,2,3,4,5] # iterate over list elements for item in ls: # print odd elements if item % 2 != 0: print(item)0 parameter of the # create a list ls = [1,2,3,4,5] # iterate over list elements for item in ls: # print odd elements if item % 2 != 0: print(item)1 function. For example, let’s use a comma as a separator. # create a list ls = [1,2,3,4,5] # use * to unpack list items print(*ls, sep=",") Output: 1,2,3,4,5 We get the elements separated by a comma. Using string 1 2 3 4 55 function to print a listThe string join() function is commonly used to concatenate elements in a list of strings to a single string. You can also use it to print elements in a list provided you convert the elements to string type before the join operation. 1 2 3 4 50 Output: 1,2,3,4,5 Here we used a list comprehension to build a list of strings and then applied the string 1 2 3 4 55 function to print the list of elements separated by a comma as a single string. Similar to the loop example, you can also make additional formatting changes. For example, print real numbers only up to two decimal places. 1 2 3 4 52 Output: 1 2 3 4 53 The numbers are printed with two decimal places. Using the format string converts the real numbers to string and thus there’s no need to convert it to a string again.
Author
|