in Technology 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
    What Advantages Do Numpy Arrays Offer Over (nested) Python Lists?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    List the advantages NumPy Arrays have over (nested) Python lists?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    What is the difference between tuples and lists in Python?...
asked Nov 23, 2020 in Technology by JackTerrance
0 votes
    To create sequences of numbers, NumPy provides a function __________ analogous to range that returns arrays instead ... answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    What are lists and tuples? What is the key difference between the two?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What's the difference? What are the advantages/disadvantages of tuples/lists? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    >>> a=[1,2,3] >>> a.remove(2) >>> a [1, 3] >>> a=[1,2,3] >>> del a[1] >>> a ... above three methods to remove an element from a list? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have two lists in Python, like these: temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One' ... ways without cycles and checking? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I am having two lists list1 = ['Apple', 'Orange', 'Grapes', 'Banana'] list2 = ['Apple', 'Orange'] I want to ... = ['Grapes', 'Banana'] Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How to make a flat list out of list of lists in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    How will you compare two lists in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    I have a list of files in a folder in my system file_list= ["A", "B", "C"] I Have read the files using ... C How do I accomplish this? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have a series of lists, call them A, B, C, D, E. Now every list has 5 elements with ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
...