What are Arrays?
An array is a vector containing homogeneous elements i.e. belonging to the same data type. Elements are allocated with contiguous memory locations. Typically the size of an array is fixed. Th e insertion and deletion costs are high as compared to the list however indexing is faster in the Arrays due to contiguous memory allocation. Arrays can be used by importing the array module.
Example:
In this example, we will create a Python array by using the array() function of the array module and see its type using the type() function.
Python3
# importing "array" for array creations import array as arr # creating an array with integer type a = arr.array( 'i' , [ 1 , 2 , 3 ]) print ( type (a)) for i in a: print (i, end = " " ) |
Output:
<class 'array.array'>
1 2 3
Difference between List and Array in Python
In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.
Contact Us