1

Are lists and arrays different in python?
Many articles refer to the following as an array: ar = [0]*N of N element. Which is a list. Not sure if these words are used interchangeably in python. And then there is a module called array

asked Oct 8, 2022 at 15:03
2
  • 2
    Arrays are rarely used in Python, and any tutorial saying "array" almost surely means "list". "List" is the proper term. I mean, technically "array" isn't entirely wrong since lists are implemented using C arrays in CPython, but that's not what they mean. Commented Oct 8, 2022 at 15:06
  • This question is similar to: Python list vs. array – when to use?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 16, 2024 at 19:52

1 Answer 1

1

The terms "list" and "array" are not interchangeable in either Python or computer science (CS).

In CS an array data structure is defined, as you've already noted, as a contiguous block of data elements all of which are the same size. Python's list does not conform to this definition as each element of a Python list can not only be a completely different data type, but can even be another data structure type such as list, dictionary, or set.

Python stores each element of a list as a separate data object, and the references to those objects are stored in Python's list data type. My past reading indicates that the list is stored as an array, but I've measured the list's time complexity and I suspect that it may technically be a hash table.

Python's lists may be ordered, and you can use and access a Python list as if it were an array as I frequently do, but don't be confused by Python's use of square brackets: it's not an array.

Python's array module implements an actual array data type for storing numerical data (actually, I think you can store character data as well).

answered Oct 8, 2022 at 19:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.