Introduction to Python Initialize List
In this article we will see initialization of lists in Python. As we know that lists are mutable and can have zero or more elements in the list and in Python, initializing a list is defined as declaring the list which creates a container where the list can be stored and then assign the name to the declared list and this process is known as initialization of lists and assigning the values to the created list is known as assignment. There are different ways for initializing lists in Python such as using square empty brackets, for loop, while loop, etc.
Working of Initialization of Lists in Python with Examples
Let us see various ways of initialization of lists such as use of square brackets, use of for loop, use of while loop, using list comprehension.
Given below shows each of these methods in detail with examples:
1. Initializing list using square brackets
We can initialize lists with empty square brackets [] while declaring the lists.
Example:
Code:
print("Program to demonstrate initialization of list using square brackets")
print("\n")
courses = []
print("The list initialized and the type of the declared variable is as follows:")
print(courses)
print(type(courses))
print("\n")
Output:
In the above program, we can see variable “courses” is declared and assigned to empty square brackets to initialize list with the name as “courses” and we can insert or append any number of elements or items to this list. In the above screenshot we can see the empty list is printed along with the type of the variable that is “list”.
To create and initialize empty list using square brackets we can also use for loop along with append() method of list to add or insert the items in the initialized list.
Let us see the demonstration of using both these loops in the below example.
Example:
Code:
print("Program to demonstrate initialization of list using for loop")
print("\n")
courses = []
for n in range(3):
courses.append('educba')
print("The list obtained after appending the value is as follows:")
print(courses)
print(type(courses))
Output:
In the above program, we are trying to initialize list with square brackets and add the elements using for loop.
2. Initializing using list() method
There is another way for creating empty list using list() method. This method is also similar to the above method, but it is most recommended to use square brackets than using list() method.
Example:
Code:
print("Program to demonstrate initialization of list using list() method")
print("\n")
courses = list()
print("The list initialized and the type of the declared variable is as follows:")
print(courses)
print(type(courses))
print("\n")
Output:
In the above program, we can see we have declared a variable “courses” where we are initializing list using list() method. Therefore we can see in the above screenshot it prints the empty list along with its type of the variable.
3. Initialization of list using list comprehension method
In this method we see range method in the for loop to create and initialize list. This method can be used to create any iterable other list using the existing iterable object such as range().
The syntax to be followed for this method is:
variable_name = [ items iteration filter]
- items: This will hold the elements of the list or transform.
- iteration: This will consider for loop iteration.
- filter: This usually holds generally an if-else statement.
Example:
Code:
print("Program to demonstrate initialization of list using list comprehension")
print("\n")
courses = [n for n in range(5)]
print("The list initialized and the items in the given list are as follows:")
print(courses)
print(type(courses))
print("\n")
Output:
In the above program we can see we are declaring variable “courses” and is initialized as it is type using list comprehension method with range() method. In the above screenshot we can see we are trying to print n numbers from ranging 0 to 4 that is 5 elements in the list. To get the same values in the list then we can write the value of n at the beginning and then using for loop we can print the n value for 5 times.
This list comprehension method can also be used with repeat() method for printing the same values in the list or to initialize and create list of lists. This repeat() method can be imported from itertools module in Python library.
Example:
Code:
from itertools import repeat
print("Program to demonstrate initialization of list using list comprehension")
print("\n")
courses = ['educba' for x in repeat(None, 5)]
print("The list initialized and the items in the given list are as follows:")
print(courses)
print(type(courses))
print("\n")
Output:
In the above program, we can see we are using repeat() method which returns the given object for the specified number of times and in the above program we have specified it as 5 times. This method is first imported by itertools module. Then we have specified the item value to be printed same as the number of times specified and the item value here, we are printing is “educba”.
4. Initialization of list using list multiplication
In this method we use * operator or list multiplication method for initializing the list. This method is one of the fastest method. This method is also similar to list comprehension method with repeat() method. This method is also used when we want to initialize and create a list with specific number of redefined values of the items in the list.
Example:
Code:
print("Program to demonstrate initialization of list using list multiplication")
print("\n")
courses = ['educba'] * 5
print("The list obtained after using * operator to print the same values is as follows:")
print(courses)
print(type(courses))
Output:
In the above program, we can see we are using * operator when we are initializing and creating list “courses”. This method prints the item value the number of times it is specified after the * operator as seen in the above screenshot.
Conclusion
In this article, we saw various ways of initializing lists in Python programming language. In Python, initialization of lists is crucial role when dealing with lists. In this article, we saw how to initialize list using square brackets which will initialize the empty list and the items can be added using for loop and append() method. In this we also how the initialization of list is done using list() method, list comprehension with range() and repeat() method and also we saw initialization of list using list multiplication with * operator.
Recommended Articles
This is a guide to Python Initialize List. Here we discuss the introduction to Python Initialize List along with the working of initialization list and examples. You may also have a look at the following articles to learn more –