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
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library. Arrays are used to store multiple values in one single variable: ... An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-arrays
Python Arrays - GeeksforGeeks
1 week ago - ... In Python, an array is used to store multiple values or elements of the same datatype in a single variable. The extend() function is simply used to attach an item from iterable to the end of the array.
Discussions

How do I declare an array in Python? - Stack Overflow
So, arrays are more efficient for large datasets of numbers. 2019-07-14T02:30:58.24Z+00:00 ... I think you (meant)want an list with the first 30 cells already filled. So ... An example to where this could be used is in Fibonacci sequence. See problem 2 in Project Euler ... That's a rather baroque way of initialising a list. Try f = [0] * 30 instead. 2010-12-18T06:24:00.68Z+00:00 ... Is not the same than a = range(10) from @slehar anwer? Love python... More on stackoverflow.com
🌐 stackoverflow.com
Python arrays
If you're just learning, just use lists. They have essentially the same interface (meaning everything you can do to an array, you can do to a list), and while they are somewhat less performant, if you're just poking around, that's probably not important. If the performance is a consideration, use NumPy. If you want really fine control over exactly how memory is laid out, use a different language. More on reddit.com
🌐 r/learnpython
13
3
March 3, 2025
I need help with arrays
Another problem you have is if i== 2 or 4 or 7: as it doesn't work the way you think it does. Replace it with if i in {2, 4, 7}: and it'll work. On another note, these are technically not arrays but lists. "What's the difference?", I hear you ask; arrays are homogeneous, lists are heterogeneous. In practice this means arrays contain data of a single type, while lists can contain data of multiple types. Python does have arrays, but the built-in one you basically never see used (array.array) and Numpy arrays aren't built-in though they're common in heavy number crunching workloads. EDIT: As for how I'd write this code; data = [] for idx in range(10): if idx in {2, 4, 7}: data.append('orange') else: data.append('white') for word in data: print(word) That first loop could further be condensed into data = [ 'orange' if idx in {2, 4, 7} else 'white' for idx in range(10) ] or, as a one-liner (though not preferably): data = ['orange' if idx in {2, 4, 7} else 'white' for idx in range(10)] More on reddit.com
🌐 r/learnpython
40
137
April 28, 2020
Are arrays and lists essentially the same?
“Array” is an ambiguous term in Python and best not used. Most beginners use it to refer to the list type, but are unaware that there is actually an array.array type in the standard library as well. More on reddit.com
🌐 r/learnpython
38
26
May 7, 2022
🌐
AskPython
askpython.com › home › python array – 13 examples
Python Array - 13 Examples - AskPython
February 16, 2023 - Python array module can be used to create arrays for integers and floats. There is no array data structure in Python, Python array append, slice, search, sort.
🌐
Mimo
mimo.org › glossary › python › arrays
Python Arrays: Syntax, Usage, and Examples
List comprehensions are a powerful way to work with arrays in Python.
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:

🌐
Reddit
reddit.com › r/learnpython › python arrays
r/learnpython on Reddit: Python arrays
March 3, 2025 -

I'm following roadmap.sh/python to learn python. I'm at the DSA section.

I understand the concept of arrays in DSA, but what I don't understand is how to represent them in Python.

Some tutorials I've seen use lists to represent arrays (even though arrays are homogeneous and lists are heterogeneous).

I've seen some other tutorials use the arrays module to create arrays like array.array('i') where the array has only integers. But I have never seen anybody use this module to represent arrays.

I've also seen some tutorials use numpy arrays. Which I don't understand their difference from the normal arrays module.

What should I use to represent arrays?

So far I've solved a couple of arrays exercises using Python lists.

🌐
Python
docs.python.org › 3 › library › array.html
array — Efficient arrays of numeric values
A new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a bytes or bytearray object, a Unicode string, or iterable over elements of the appropriate type.
Find elsewhere
🌐
Readthedocs
pythontutorial-wdyds.readthedocs.io › en › latest › 3_Lists_Arrays › lists_arrays.html
4. Lists and Arrays — A Python Tutorial for Data Scientists 2.0 documentation
Thankfully, this is where Python has a native list type and where we’ll introduce the extremely useful numpy array, (numpy.ndarray) to handle many data points. These data types are similar to those you just learned about except that now we have to worry about where our data is and not just what it is. For example, if you have loaded into a variable the air temperature of Evanston, Illinois for the last 100 years, you might want to do an analysis of just the summer highs over time, so how do you access just those data points?
🌐
Programiz
programiz.com › python-programming › array
Python Array of Numeric Values
In this tutorial, you’ll learn about Python array module, the difference between arrays and lists, and how and when to use them with the help of examples.
🌐
Tutorialspoint
tutorialspoint.com › home › python › python arrays
Python Arrays
February 21, 2009 - Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9. To create an array in Python, import the array module and use its array() function.
🌐
Educative
educative.io › blog › array-operations-in-python
Array operations in Python
What is an array?Creating an arrayHow to access array elements in PythonSlicing Python arraysDifferent array operationsLength of the arrayUpdating array elementsAdding elements to an arrayAdding new elements to the arrayDeleting elements from an arrayExercise: Array managementExpected outputSearching an elementThe in keywordThe index() methodReverse an arrayUsing [::-1] slicingUsing reverse() methodCounting elements in the array
🌐
Edureka
edureka.co › blog › arrays-in-python
Arrays in Python: What are Python Arrays & How to use them? | Edureka
November 27, 2024 - This article on Arrays in Python talks about Array fundamentals like functions, lists vs arrays along with its creation and various other basic operations.
🌐
freeCodeCamp
freecodecamp.org › news › how-arrays-work-in-python
How Arrays Work in Python – Array Methods Explained with Code Examples
July 12, 2023 - From the code above, we have an array numbers with elements [1, 2, 3, 5, 6]. We want to insert the number 4 at index 3 (which is the fourth position in the array, as Python is 0-indexed).
🌐
CodeChef
codechef.com › blogs › arrays-in-python
Arrays in Python (With Examples and Practice)
July 11, 2024 - Learn about Arrays, the most common data structure in Python. Understand how to write code using examples and practice problems.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.02-the-basics-of-numpy-arrays.html
The Basics of NumPy Arrays | Python Data Science Handbook
Data manipulation in Python is nearly synonymous with NumPy array manipulation: even newer tools like Pandas (Chapter 3) are built around the NumPy array. This section will present several examples of using NumPy array manipulation to access data and subarrays, and to split, reshape, and join ...
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:
🌐
W3Schools
w3schools.com › python › python_dsa_lists.asp
Python Lists and Arrays
For example, an algorithm can be used to find the lowest value in a list, like in the example below: Create an algorithm to find the lowest value in a list: my_array = [7, 12, 9, 4, 11, 8] minVal = my_array[0] for i in my_array: if i < minVal: ...
🌐
New York University
physics.nyu.edu › pine › pymanual › html › chap3 › chap3_arrays.html
3. Strings, Lists, Arrays, and Dictionaries — PyMan 0.9.31 documentation
Therefore, we distinguish between array multiplication and matrix multiplication in Python. Normal matrix multiplication is done with NumPy’s dot function. For example, defining d to be the transpose of c using using the array function’s T transpose method creates an array with the correct dimensions that we can use to find the matrix product of b and d:
🌐
Real Python
realpython.com › python-array
Python's Array: Working With Numeric Data Efficiently – Real Python
December 22, 2023 - To whet your appetite, here’s what the basic syntax for creating an array in Python looks like: ... After importing the array class from the array module, you must at least specify the type code for your array and optionally provide an initializer value. For example, this will create an array of four consecutive integers:
🌐
Bhrighu
bhrighu.in › blog › array-in-python-examples-and-comparison
Array in Python: Full Guide with Examples & Comparison
While Python does not have a built-in array data type in the same way as some other languages, it offers multiple ways to work with array-like structures depending on the application. This blog explores the concept of Python arrays, their implementations, and practical usage through clear examples ...