0 votes
in Python by
What is the difference between Python Arrays and lists?

1 Answer

0 votes
by

Arrays in python can only contain elements of same data types i.e., data type of array should be homogeneous. It is a thin wrapper around C language arrays and consumes far less memory than lists.

Lists in python can contain elements of different data types i.e., data type of lists can be heterogeneous. It has the disadvantage of consuming large memory.

import array

a = array.array('i', [1, 2, 3])

for i in a:

    print(i, end=' ')    #OUTPUT: 1 2 3

a = array.array('i', [1, 2, 'string'])    #OUTPUT: TypeError: an integer is required (got type str)

a = [1, 2, 'string']

for i in a:

   print(i, end=' ')    #OUTPUT: 1 2 string

Related questions

0 votes
asked Sep 9, 2022 in Python by john ganales
0 votes
asked Jun 12, 2020 in Python by Robindeniel
...