Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
list
python

Arrays vs. Lists in Python

Educative Answers Team

In Python, both arrays and lists store data that can be indexed and iterated through. However, there is a sig​nificant difference between the two data structures.

Lists

  • Python lists are very flexible and can hold arbitrary data.

  • Lists are a part of Python’s syntax, so they do not need to be declared first.

Arrays

  • Python arrays are just a thin wrapper on C arrays.

  • Arrays need to first be imported, or declared, from other libraries (i.e. numpy).

Example 1:

The examples below click the tabs to view the syntax:

my_list = ["Jon", 318, 'C', False]
print(my_list)

Lists cont.

  • Lists can also be re-sized quickly in a time-efficient manner. This is because Python initializes some extra elements in the list at initialization.

  • Lists can hold heterogeneous data.

Arrays cont.

  • Arrays cannot be resized. Instead, an array’s values would need to be copied to another larger array.

  • Arrays can only store homogenous data. They have values with uniform data types.

svg viewer
A Python List. The elements can have different data types.
svg viewer
A Python Array. All elements have the same data type.
  • Mathematical functions cannot be directly applied to lists. Instead, they have to be individually applied to each element.
  • Arrays are specially optimized for arithmetic computations.

Example 2:

See the examples in each tab below to compare:

my_list = [6, 8, 14, 2]

for i in range(0,len(my_list)):
  my_list[i] = my_list[i] / 2

print(my_list)

List cont.

  • Lists consume more memory as they are allocated a few extra elements to allow for quicker appending of items.

Arrays cont.

  • Since arrays stay the size that they were when they were first initialized, they are compact.

RELATED TAGS

array
list
python
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring