How To Print List In Python?

In this blog post, we will show you how to print a list in Python. We will go over different ways to print a list, and how to format the output.

Checkout this video:

Introduction

In Python, a list is an ordered collection of objects. Lists are mutable, meaning they can be changed. You can print the whole list on one line using print(). Or you can use a for loop to print each item on a separate line.

Printing a List

Python Lists can be printed in two ways. The conventional way and the enumerate way.

The conventional way to print a list in Python is to simply iterate through it and print each element separately. However, this can sometimes be inefficient if you need to print a very large list. In such cases, it might be better to use the enumerate function which will automatically keep track of the index of each element in the list.

To print a list in the conventional way, you can simply use a for loop:

“`
for element in my_list:
print(element)
“`
If you need to print a very large list, it might be better to use the enumerate function:
“`
for i, element in enumerate(my_list):
print(i, element)

Creating a List

Lists are one of the four mutable data types in Python. A list is a container that can hold different types of data in it. They are declared by putting all the items (elements) inside square brackets [] separated by commas. For example,

list1 = [‘physics’, ‘chemistry’, ‘maths’]
list2 = [1, 2, 3, 4, 5 ]
list3 = [“a”, “b”, “c”, “d”]
Unlike other data types such as tuple and set which are immutable (cannot be changed), lists are mutable which means they can be changed. We will see how to perform various operations on a list like adding or removing elements from it.

Adding Elements to a List

There are a number of ways you can add elements to an existing list in Python.

If you want to add a single element to the end of a list, you can use the append() method. This will add the new element to the end of the list:

myList = [1,2,3]
myList.append(4)
print(myList)
This would output: [1, 2, 3, 4]

You can also use the extend() method to add multiple elements to the end of a list at once:

myList = [1,2,3]
myList.extend([4,5])
print(myList)
This would output: [1, 2, 3, 4, 5]

Deleting Elements from a List

To delete an element from a list, you can use the remove() or pop() methods.

The remove() method takes an element as an argument and removes the first occurrence of that element from the list.
If the element is not found in the list, an error is raised.

The pop() method takes an index as an argument and removes the element at that index. If no index is specified, it removes and returns the last item in the list.

List elements can also be deleted with the del statement. This can be used to delete a single item or multiple items.

To delete a single item, you can use its index:

del my_list[0]

To delete multiple items, you can use the slice operator:

del my_list[0:2]

Looping Through a List

There are a number of ways to loop through a list in Python. One way is using the indices of the list elements. Another way is by using the for-in loop.

If you want to loop through a list, you can use the for-in loop. The syntax is as follows:

for element in list:
statement(s)
This will execute the indented statement(s) once for each element in the list. For example, if we have a list of numbers, we can use the for-in loop to print each element in the list:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
This will print each number in the list on a separate line.

Slicing a List

Slicing a list is pretty simple. It’s just like slicing a string. You can use the slice method on lists:

>>> my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> my_list[2:8]
[2, 3, 4, 5, 6, 7]

Merging Lists

List data types have some more methods. Lists are mutable, meaning they can be changed. Some list methods are:

list.append(elmnt)
Adds an element elmnt to the end of a list.

list.extend(seq)
Appends the contents of seq to the end of the list. Typical usage to make a list longer by adding elements from another list:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)

Copying a List

There are numerous ways to copy a list in Python. The easiest way is to use the built-in List method copy(). you can also use the built-in list() function. For example, if you have aList = [1, 2, 3] then list(aList) will return [1, 2, 3]. You could also use the slice operator to make a copy of a list. For example, if you have aList = [1, 2, 3] then aList[:] will return [1, 2, 3].

Conclusion

There is more than one way to print a list in Python. The most common is the for loop. This will print each element of the list on a separate line. You can also use the+ Operator to print all the elements of a list on one line.

Scroll to Top