What is the Difference Between Array and List in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What is an Array in Python?

  • Arrays are not the in-built data structure readily available in Python.
  • We must import the array from the 'array' or 'numpy' module.
  • Array stores elements of similar types. If we try to store elements of different types, it will throw an error.
  • We can only store elements of the same types in the array.

What is List in Python?

  • Lists are the inbuilt data structure of python.
  • We can store elements of different types in the list.
  • Items in the list are enclosed between square brackets and separated by commas.
  • We can even nest lists with other data structures like lists, dictionaries, tuples, etc.

Examples of List and Array

Let's understand the differences between a list and an array in python by looking at basic examples of list and array.

Python List:

In the above example, we have created sample_list, which contains values of different types, and sample_nested_list contains the nested lists in itself, which are of different sizes, and also it contains numeric values as well.

Python Array:

Method 1: Using array package

Method 2: Using numpy package

In the above example, we have created the sample_array using two different methods. Method 1 uses the array package to initialize the sample array, whereas method 2 uses the numpy package to initialize the sample array. sample_array won't be able to store elements of different data types. If we try to add some string value in the sample array, it will throw an error. Also, we will not be able to have the nesting of arrays if the arrays are of different sizes, which is not the case with the list. Use this Free Python Compiler to compile your code.

Difference Between Array and List in Python

ArrayList
Need to import using array or numpy.In-built data structure.
Need to enclose using .array function.Enclosed in square brackets.
Cannot contain elements of different types.Can contain elements of different types.
Need to have the same size of the array for nesting.Variable size nesting is possible.
Can apply direct arithmetic operations.Cannot apply direct arithmetic operations.
It consumes less memory comparatively.It consumes more memory.
Need to create an explicit loop to print the array elements.Can be printed without creating an explicit loop.
Arrays offer specialized functions for numerical operations.Lists have more built-in methods and functionalities.
Arrays are optimized for numerical computations and are faster.Lists are generally slower for numerical computations.
Arrays are specialized for numerical computations and memory efficiency.Lists are more flexible for general-purpose use.

Learn More:

Want to learn more about arrays and lists in python?

Conclusion

  • List is an in-built data structure, whereas, for an array, we need to import it from the array or numpy package.
  • Lists and arrays both are mutable and store ordered items.
  • List can store elements of different types, but arrays can store elements only of the same type.
  • List provides more flexibility as it doesn't require explicit looping, but arrays require explicit looping to print elements.
  • We can't apply arithmetic operations directly in the list, but we can apply arithmetic operations directly on arrays.

Elevate your Python expertise with our top-rated FREE Python certification course. Join now and get certified!