AppDividend
Latest Code Tutorials

Different Ways to Initialize List in Python

0

Python list is an ordered collection of values. The list can hold various types of values. The list is a mutable(changeable) container. That means that we can insert, delete, or modify existing values.

Python list represents a mathematical concept of the finite sequence. The values of the list are called items or elements. Python list can contain the same value multiple times, unlike set. Each occurrence is considered a different item.

There are the following ways to initialize the list.

  1. Initialize a list using square brackets.
  2. Initialize using list() function.
  3. Using list comprehension
  4. Using list multiplication

Initialize a List Square Brackets

You can use square brackets to initialize an empty list in Python.

If you want to create the empty list with no values, there are two ways in which you can initialize your list with empty values.

First, you have to declare a list with no values by specifying the set of square brackets without any element values. See the following code.

data = []
print(data)

Output

[]

Let’s initialize the list using some numeric values.

data = [11, 19, 21, 29, 46]
print(data)

Output

[11, 19, 21, 29, 46]

Initialize a List Using list()

Python list() function creates the list from the iterable object.

An iterable may be either a container, a sequence that supports iteration, or an iterator object. If no parameter is specified, then a new empty list is created.

Another way to create and initialize the list with no values is to use the list() method.

data = list()
print(data)

Output

[]

Now, let’s create an empty list using [] and list() function and compare its values.

data = list()
info = []
print(data == info)

Output

True

Both of these first two approaches return the same result, which is an empty list. There are no standards for when it is best to use either of these methods, but generally speaking, the blank square brackets ( [ ] ) approach is used because it is more instructive and concise.

Initialize a List Using List Comprehension

You can also initialize the list with default values using a list comprehension approach.

In Python, list comprehension refers to the technique that allows us to create lists using the existing iterable object, such as a list or the range() statement.

List comprehension is a handy way to define a list based on an iterator because it is elegant, simple, and widely recognized.

Syntax of List comprehension

*result*  = [ *transform*    *iteration*         *filter* ]

Now, let’s see the following example.

data = [i for i in range(10)]
print(data)

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Now, from the above syntax, let’s break down the code.

result = data – Returned list.

transform = i – The items of the list.

iteration = for i in range(10)

filter = we have not defined but a generally if-else statement.

That means, we have initialized the list with 10 items starting from 0 to 9 using the list comprehension.

Using list multiplication

Another approach for initializing the list with multiple values is list multiplication.

This approach allows us to create a list with a specific number of predefined values.

See the following code.

data = ['x'] * 10
print(data)

Output

['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']

From the output, our program has created a list that contains ten values. 

Let’s break down the code.

On the first line, we have used the multiplication syntax to declare the list with ten values. The value we use for each item in the list is ‘x’ or whatever element you want to add. Then, we use the print() method to print the list to the console.

Conclusion

Initializing the list is a core part of working with lists. This example discussed how to initialize the empty lists using the square brackets[ ] and the list() method. We have also discussed how to use the list comprehension list multiplication techniques to create the list containing a certain number of values.

See also

Python list append()

Python list extend()

Python list sort()

Python list replace

Python list contains

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.