There are various techniques in Python to initialize a list object. We can use Square brackets, list() function, multiply a list object or use the list comprehension and declare a list object with multiple elements.
A list is an inbuilt Python data structure, which can store multiple elements in sequential order using indexing. A Python list is mutable by nature, which means we can change or replace the values of elements contained in the list.
The index value of list elements starts from 0 up to n-1, where n is the total number of elements present in the list. Suppose if a list has 5 elements, the first element will have the index value of 0 and the last element will have the index value 4. With the help of these index values, we can access individual elements from the list with ease.
This tutorial is not about Python lists, here we will only discuss the different techniques to initialize a list object in a Python program. Will will cover all the methods you can use to declare a list object and also discuss how you may use those techniques.
Techniques to initialize a List in a Python Program
There are four major ways to initialize a Python list
- Using Square brackets
- Using list() function
- Using list manipulation
- Using List comprehension
1. Initialize a Python list: Using Square brackets
The most common way to initialize a Python list is using square brackets. We can pass all the elements inside the square bracket separated with commas and assign that bracket to a variable name using the assignment operator.
list_name = [item1, item2, item3, item4,...., itemn]
# initialize a list using square bracket my_bucket_list = ['Indoor Skydive','Play PS5', 'Go Hiking', 'Scuba Dive','Sleep in open Sky' ] print(my_bucket_list)
['Indoor Skydive', 'Play PS5', 'Go Hiking', 'Scuba Dive', 'Sleep in open Sky']
When to initialize a list using a square bracket?
Often in Python programming, we use square brackets to initialize a list object. We only use a square bracket when we want to initialize a fixed number of elements in a list.
2. Initialize a Python list: Using list() method
Python also provide a
list() function that accepts an iterable object and converts it into a list object.
list() function we can initialize a list object or convert another iterable object to a Python list.
list_name = list(iterable_object)
let’s say you have a string object and you want to create a list object containing all the elements of the string. For that, you can use the list() function.
message = "Hello World" # initialize a list using list() function message_chracters = list(message) print(message_chracters)
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
When to initialize a list using a list() function?
list() function is mostly used during type conversion or to initialize an empty list object. Many Python functions such as
map() return iterable objects, and to convert those objects into a readable list we use the
# initialize an empty list using list() function empty_list = list() # initialize a list containing values range 0 to 99 list_100 = list(range(100)) print("Empty List: ",empty_list) print("100 values list:", list_100)
Empty List:  100 values list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
3. Initialize a Python list: Using list Manipulation
We can multiply a list object with an integer number to create a new list object that contains a specific number of elements. With this, we can create a list object that has a fixed size.
list_name = [item1, item2,.... itemn]* integer_number
# initialize a list using list manipulation my_list = *7 print(my_list)
[1, 1, 1, 1, 1, 1, 1]
When to initialize a list using list manipulation?
When we want a list object with a fixed size and default values. there we use the list manipulation to decare a list.
Then later we can change the default values of the list and but we already have a fixed size list object.
# initialize a list using list manipulation eight_names = ["No Name"]*8 print(eight_names)
3. Initialize a Python list: Using List comprehension
List comprehension is a technique to create a new list object using a for loop and iterable object within a single line of code statement.
List comprehension is a shorthand for
for loop and list
list_name = [element for element in iterable object]
message = "Hello World" # initialize a list using list comprehension message_list = [element for element in message] print(message_list)
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
When to initialize a list using list comprehension?
List comprehension is mostly used when we want to create or initialize a list based on an existing iterable object.
We can also put some conditions in the list comprehension to filter out the elements that we want to store in our list.
message = "Hello World" # initialize a list using list comprehension only_vowels = [element if element in "aeiouAEIOU" else "-" for element in message] print(only_vowels)
['-', 'e', '-', '-', 'o', '-', '-', 'o', '-', '-', '-']
Initializing a list is the first step toward working with a list object. In this tutorial, we discussed the different ways to initialize a list object in Python. We learned how can we initialize a list using square bracket, list manipulation, list comprehension and list() function.
The square bracket technique is used when we want to create a small list with some values. List comprehension and list manipulation are used when we want to create a list object with a fixed number of elements. And the list() function is majorly used for type conversion, one object to list object.
People are also reading:
- Python: How to Initialize List of Size N
- Replace Item in Python List
- Python List or Array
- How do you remove duplicates from a Python list while preserving order?
- Python List Files in a Directory: A Complete Guide
- List append vs extend method in Python
- Python list vs Tuple
- List Comprehension in Python
- Python List
- List Methods in Python