How to Declare a List in Python?

Posted in /  

How to Declare a List in Python?
vinaykhatri

Vinay Khatri
Last updated on April 26, 2024

    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.

    Python List

    A list is an inbuilt Python data structure that 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 of 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

    1. Using Square brackets
    2. Using list() function
    3. Using  list manipulation
    4. 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.

    Syntax

    list_name = [item1, item2, item3, item4,...., itemn]

    Example

    # 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)

    Output

    ['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 the list() method

    Python also provides a list() function that accepts an iterable object and converts it into a list object. Using the list() function we can initialize a list object or convert another iterable object to a Python list.

    Syntax

    list_name = list(iterable_object)

    Example

    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)

    Output

    ['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 zip() , enumerate() , filter() , range() and map() return iterable objects, and to convert those objects into a readable list, we use the list() function.

    Example

    # 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)

    Output

    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.

    Syntax

    list_name = [item1, item2,.... itemn]* integer_number

    Example

    # initialize a list using list manipulation
    my_list = [1]*7
    
    print(my_list)

    Output

    [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 list manipulation to declare a list. Then later, we can change the default values of the list and but we already have a fixed-size list object.

    Example

    # 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 an iterable object within a single line of code statement. List comprehension is a shorthand for for loop and list append() method.

    Syntax

    list_name = [element for element in iterable object]

    Example

    message = "Hello World"
    
    # initialize a list using list comprehension
    message_list = [element for element in message]
    
    print(message_list)

    Output

    ['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.

    Example

    message = "Hello World"
    
    # initialize a list using list comprehension
    only_vowels = [element if element in "aeiouAEIOU" else "-" for element in message]
    
    print(only_vowels)

    Output

    ['-', 'e', '-', '-', 'o', '-', '-', 'o', '-', '-', '-']

    Conclusion

    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 we could initialize a list using square brackets, list manipulation, list comprehension, and the 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:

    Leave a Comment on this Post

    0 Comments