1.

What is the difference between Python Arrays and lists?

Answer»
  • 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 arraya = array.array('i', [1, 2, 3])for i in a: print(i, end=' ') #OUTPUT: 1 2 3a = 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


Discussion

No Comment Found