Python List Vs Python Array in a nutshell

Python List Vs Python Array in a nutshell

Python List is amazing

Introduction:

Do you know that Python has a built-in array module to declare arrays? You don't have to install NumPy, unless you are not playing with complex array and nD arrays.

Python List:

Python List is a built-in data structure, which can contain one or more data types, like integer, float, string etc.

You can even store a list inside a list, i.e. nested lists.

my_empty_list = []

my_list = [1, 'Gaurav', [2, 4, 6]]

But Python lists consume large memory size for data but on the other hand these are flexible, efficient for CRUD operations.

Python Array:

Python Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.

In Python, the type is specified at object creation time by using a type code, which is a single character. We can create a Python Array using Python's built-in array module

import array

# Syntax: array.array(typecode, [values])

my_empty_array = array.array('i', [1])

my_array = array.array('i', [1, 23, 45])

Python Array takes less space in memory, supports negative indexing but at the same time, can only store same data types, less flexible than lists, because modifications are expensive

For more on Python Arrays, you can check out official documentation link here: https://docs.python.org/3/library/array.html

For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.

Did you find this article valuable?

Support Dev.Junction by becoming a sponsor. Any amount is appreciated!