Python
Container Types
list
create

Declare a Python list: Syntax and Examples

In this article, we'll look at different ways to declare lists in Python.

Here's what we'll cover:

  • Declaring and initializing an empty list
  • Declaring a two-dimensional list
  • Declaring lists using the range() and for statements
  • Declaring a list of a given length
  • Declaring lists of strings, classes, and more
  • Declaring lists of global variables

1. Declaring an empty list

There are several ways to declare and initialize an empty list in Python. Here are some examples

1.1. Using parentheses

First, you can declare it using empty parentheses, like this

my_list = []

1.2. Using the list() function

Here's how to initialize with an empty list using the list() function.

my_list = list()

2. Declaring a two-dimensional empty list

When creating a two-dimensional empty list, you can also use square brackets to define it, as follows

matrix = [ [], [] ]

Most often, they are created by specifying the number of rows and columns.

2.1. Declaring a two-dimensional list with a for statement

To do this, you can use a for loop like this

rows = 3
cols = 4
 
my_list = []
for _ in range(rows):
    inner_list = []
    for _ in range(cols):
        inner_list.append(None)
    my_list.append(inner_list)

2.2. Declaring a two-dimensional list using list compression

Alternatively, you can write simpler code using list comprehensions.

rows = 3
cols = 4
 
my_list = [[None for _ in range(cols)] for _ in range(rows)]
 
# Output
[
    [None, None, None, None],
    [None, None, None, None],
    [None, None, None, None]
]

3. Declare a list of any length with the range() function

A Python list is not a data type whose length can be specified or bounded. To declare a list of a certain length, you must use a dummy value instead.

The sample code below declares a list of arbitrary length by filling it with 0. You can use the range() function to write code like this.

list_count = 5
list_5 = [ 0 for _ in range(list_count) ]
 
# output
[0, 0, 0, 0, 0]

3.1. Declaring a list of specific strings or classes

Using the same method as above, you can easily declare a list of a given string or class of any length.

For example, you can iterate over a given string, or create a list of

list_count = 5
list_5_a = [ 'a' for _ in range(list_count) ]
 
# output
['a', 'a', 'a', 'a', 'a']

You can also use the chr() function to represent strings that are different from the following,

row = 4
col = 3
 
matrix = [ [chr(97 + i + j) for i in range(col)]  for j in range(row)]
 
# output
[['a', 'b', 'c'],
 ['b', 'c', 'd'],
 ['c', 'd', 'e'],
 ['d', 'e', 'f']]

Lists of specific classes can also be declared.

class Student:
    def __init__(self, age):
        self.age = age
 
    def __repr__(self):
        return f'Student(age:{self.age})'
 
    def __str__(self):
        return f'Student(age:{self.age})'
 
list_count = 5
matrix  = [ Student(age) for age in range(list_count) ]
 
# output
[Student(age:0), Student(age:1), Student(age:2), Student(age:3), Student(age:4)]

4. Declare a list of global variables

Python does not have a separate function for declaring global variables; all variables declared in the top-level scope are available as global variables.

If you need to declare a specific list as a global variable, you can use it in other scopes, such as functions, by prefixing the list variable with the global keyword.

# init as global
my_list = []
 
def add_to_list(value):
    global my_list
    my_list.append(value)
 
add_to_list(1)
add_to_list(2)
print(my_list)
 
# Output: [1, 2]

Conclusion

We've seen several different ways to declare a Python list. It's simple stuff, but it's also the basis of all data structure declarations.

I hope this post helps you in your actual development.

copyright for Declare a Python list: Syntax and Examples

© 2023 All rights reserved.