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:
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:
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):
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]
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]
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:
To delete multiple items, you can use the slice operator:
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:
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:
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]
[2, 3, 4, 5, 6, 7]
List data types have some more methods. Lists are mutable, meaning they can be changed. Some list methods are:
Adds an element elmnt to the end of a list.
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]
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].
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.