variable = []

Now variable refers to an empty list*.

Of course this is an assignment, not a declaration. There's no way to say in Python "this variable should never refer to anything other than a list", since Python is dynamically typed.


*The default built-in Python type is called a list, not an array. It is an ordered container of arbitrary length that can hold a heterogenous collection of objects (their types do not matter and can be freely mixed). This should not be confused with the array module, which offers a type closer to the C array type; the contents must be homogenous (all of the same type), but the length is still dynamic.

Answer from sepp2k on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ array.html
array โ€” Efficient arrays of numeric values
This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ...
Top answer
1 of 16
426
variable = []

Now variable refers to an empty list*.

Of course this is an assignment, not a declaration. There's no way to say in Python "this variable should never refer to anything other than a list", since Python is dynamically typed.


*The default built-in Python type is called a list, not an array. It is an ordered container of arbitrary length that can hold a heterogenous collection of objects (their types do not matter and can be freely mixed). This should not be confused with the array module, which offers a type closer to the C array type; the contents must be homogenous (all of the same type), but the length is still dynamic.

2 of 16
196

This is surprisingly complex topic in Python.

Practical answer

Arrays are represented by class list (see reference and do not mix them with generators).

Check out usage examples:

# empty array
arr = [] 

# init with values (can contain mixed types)
arr = [1, "eels"]

# get item by index (can be negative to access end of array)
arr = [1, 2, 3, 4, 5, 6]
arr[0]  # 1
arr[-1] # 6

# get length
length = len(arr)

# supports append and insert
arr.append(8)
arr.insert(6, 7)

Theoretical answer

Under the hood Python's list is a wrapper for a real array which contains references to items. Also, underlying array is created with some extra space.

Consequences of this are:

  • random access is really cheap (arr[6653] is same to arr[0])
  • append operation is 'for free' while some extra space
  • insert operation is expensive

Check this awesome table of operations complexity.

Also, please see this picture, where I've tried to show most important differences between array, array of references and linked list:

๐ŸŒ
CodingNomads
codingnomads.com โ€บ python-array-with-type
Python Array with Type
The array module, and specifically using array.array(), provides a quick way to create and manipulate arrays in Python. ... This creates an array of integers (type code 'i') with five elements, all initialized to 0.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ python array declaration: a comprehensive guide for beginners
Python Array Declaration: A Comprehensive Guide for Beginners - AskPython
April 3, 2023 - In Python, you can declare arrays using the Python Array Module, Python List as an Array, or Python NumPy Array. The Python Array Module and NumPy Array offer more efficient memory usage and specific data types, while Python lists provide ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ declare an array in python
Declare an array in Python | Sentry
from array import array my_int_array = array('i', [1, 2, 3, 4, 5]) # create an array of signed integers my_int_array.append(6) # will add 6 to the end of the array my_int_array.append('a') # will throw a TypeError: an integer is required (got type str) Sentry BlogPython Performance Testing: A Comprehensive Guide (opens in a new tab) Syntax.fmListen to the Syntax Podcast (opens in a new tab) Sentry BlogLogging in Python: A Developerโ€™s Guide (opens in a new tab)
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_arrays.asp
Python Arrays
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-create-an-array-with-a-defined-data-type-in-python
How to create an array with a defined data type in Python
Line 11: We use the array.typecode function to check the data type of the array we created. ... Line 2: We import the array module in Python.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ declaring-an-array-in-python
Declaring an Array in Python - GeeksforGeeks
July 10, 2025 - Explanation: array() from the array module creates a typed array in Python. 'i' denotes integer type.
Find elsewhere
๐ŸŒ
Cython
cython.readthedocs.io โ€บ en โ€บ latest โ€บ src โ€บ tutorial โ€บ array.html
Working with Python arrays โ€” Cython 3.3.0a0 documentation
To avoid having to use the array constructor from the Python module, it is possible to create a new array with the same type as a template, and preallocate a given number of elements. The array is initialized to zero when requested. ... from cython.cimports.cpython import array import array int_array_template = cython.declare(array.array, array.array('i', [])) cython.declare(newarray=array.array) # create an array with 3 elements with same type as template newarray = array.clone(int_array_template, 3, zero=False)
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ declare an array in python
How to Declare an Array in Python | Delft Stack
February 2, 2024 - The array is defined with the parentheses and essentially two parameters. The first parameter is a type code that defines the type of the elements, and another parameter is the list of elements enclosed in the square brackets.
๐ŸŒ
Finxter
blog.finxter.com โ€บ declare-an-array-python
How to declare an array in Python? โ€“ Be on the Right Side of Change
You can use the Numpy module to declare arrays in Python. As a matter of fact, the Numpy module has been specifically designed to work with arrays. The NumPy array contains a single data type and is optimized for numerical analysis.
๐ŸŒ
w3resource
w3resource.com โ€บ python โ€บ library โ€บ python_array_module.php
Python: array module - w3resource
Python: array - This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers and methods with examples - w3resource
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ python โ€บ python array and how to use array in python [with examples]
Python Array And How To Use Array In Python [With Examples]
April 1, 2025 - This Python Array tutorial explains what is an Array in Python, its syntax, how to perform various operations like sort, traverse, delete etc
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ blogs โ€บ types-of-arrays-in-python
Arrays in Python: Types, Examples, Declaring | Hero Vired
During declaration, the process involves specifying the data type, array name, and size. Different types of arrays in Python can be declared based on the data they will hold.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python array โ€“ define, create
Python Array โ€“ Define, Create
August 12, 2024 - What is Python Array? A Python Array is a common type of data structure wherein all elements must be of the same data type. In Python programming, an array can be handled by the "array" module. If yo
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-arrays
Python Arrays: What They Are and How to Use Them | Linode Docs
June 17, 2022 - The available types are indicated using codes, which consist of the following: Generally, though, for arrays containing numbers, you can focus on using just two of the available codes. Use the i code for arrays containing integers. Use the d code for arrays containing floating point numbers. You can see an example showing how to use a code to initiate an array at the beginning of the How to Use Arrays in Python section of this guide.
๐ŸŒ
TestMu AI Community
community.testmu.ai โ€บ ask a question
How to declare an array in Python? - TestMu AI Community
May 20, 2024 - First, you need to import the array module (import array). Then, you can create an array with a specific data type like this: my_array = array.array(โ€˜iโ€™, [1, 2, 3, 4, 5]), where โ€˜iโ€™ indicates the data type (integer in this case) ยท Another way to declare an array in Python is to use ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ array
Python Array of Numeric Values
This determines the type of the array during creation. ... We will not discuss different C types in this article. We will use two type codes in this entire article: i for integers and d for floats. Note: The u type code for Unicode characters is deprecated since version 3.3.
๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-declare-an-array-in-python
How to declare an array in Python - Studytonight
Initializers - a set of values of similar types, like, [1, 2, 3] or ["abc", "pqr", "xyz"], etc. The below code example imports the Python array module and uses it to create an array in Python. It declares an array of a set of signed integers and then prints the elements.