Javatpoint Logo
Javatpoint Logo

Python Array vs. List

Python array and lists are the important data structure of Python. Both list and array and list are used to store the data in Python. These data structures allow us to indexing, slicing, and iterating. But they have little dissimilarity with each other. In this tutorial, we will learn the essential difference between the Python list and array.

Introduction

As we know that, Python has extensive data structures such as lists, tuples, sets, and dictionaries that provide many features and functions. The lists are the most effective and easy-to-use data structure in Python.

On the other hand, Python doesn't provide built-in support to the array. We need to import the array module to use the array module or from the NumPy package in the Python program. And that's a primary difference between the array and list. Before diving deep into this topic, let's have a brief introduction to both data structures.

Python List

A list is a built-in, linear data structure of Python. It is used to store the data in a sequence manner. We can perform several operations to list, such as indexing, iterating, and slicing. The list comes with the following features.

  • The list elements are enclosed with a square bracket, and each element is separated by a comma (,).
  • It is a mutable type which means we can modify the list items after their creation.
  • The lists are ordered, which means items are stored in a specific order. We can use indexing to access the list element.
  • We can store the items of different data types. We can combine strings, integers, and objects in the same list.

Below is an example of a list.

Example -

Output:

[31, 60, 19, 12]
<class 'list'>

Example - 2

Output:

[1, 'Yash', ['a', 'e']]

In the above list, the first element is an integer; the second is a string and third is a list of characters.

Array in Python

An array is also a linear data structure that stores the data. It is also ordered, mutable, and enclosed in square brackets. It can store the non-unique items. But there are restrictions to store the different data type values.

To work with the Array in Python, we need to import either an array module or a Numpy.

Elements are allocated in contiguous memory location that allows us to easy modification, addition, deletion, accessing of element. Moreover, we need to specify the data type. Let's understand the following example.

Example -

Output:

array('i', [31, 60, 19, 12])
<class 'array.array'>

Example - 2: Using Numpy array

Output:

['numbers' 'sunil' 'sachin' 'megha' 'deepti']
<class 'numpy.ndarray'>

We have specified the string type and stored the string value.

Difference between Array and List

Now, we have a brief introduction and features. Here, we will discuss the differences between the Array and List.

Sr. No List Array
1. The list can store the value of different types. It can only consist of value of same type.
2. The list cannot handle the direct arithmetic operations. It can directly handle arithmetic operations.
3. We need to import the array before work with the array. The lists are the build-in data structure so we don't need to import it.
4. The lists are less compatible than the array to store the data. An array are much compatible than the list.
5. It consumes a large memory. It is a more compact in memory size comparatively list.
6. It is suitable for storing the longer sequence of the data item. It is suitable for storing shorter sequence of data items.
7. We can print the entire list using explicit looping. We can print the entire list without using explicit looping.
8. It can be nested to contain different types of elements. It must contain either all nested elements of same size.

Conclusion

We have discussed the differences between array and list. Both data types are essential in Python and both have some limitations. Python lists are easy to use in python where arrays are typically used for data analysis.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA