Q. Differentiate between lists in Python and arrays.


Answer :-

List :-
• List can have elements of different data types.
For example:- [1,3.4, ‘hello’, ‘a@’]
• Elements of a list are not stored contiguously in memory.
• Lists do not support element wise operations. For example:- addition, multiplication, etc. Because elements may not be of same type.
• Lists can contain objects of different datatype that Python must store the type information for every element along with its element value. Thus lists take more space in memory and are less efficient.
• List is a part of core Python.

Array :-
• All elements of an array are of same data type.
For example:- An array of floats may be: [1.2, 5.4, 2.7]
• Array elements are stored in contiguous memory locations. This makes operations on arrays faster than lists.
• Arrays support element wise operations. For example, if A1 is an array, it is possible to say A1/3 to divide each element of the array by 3.
• NumPy array takes up less space in memory as compared to a list because arrays do not require to store datatype of each element separately.
• Array (ndarray) is a part of NumPy library.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post