in Technology by
What are lists and tuples? What is the key difference between the two?

1 Answer

0 votes
by

Lists and Tuples are both sequence data types that can store a collection of objects in Python. The objects stored in both sequences can have different data types. Lists are represented with square brackets ['sara', 6, 0.19], while tuples are represented with parantheses ('ansh', 5, 0.97).
But what is the real difference between the two? The key difference between the two is that while lists are mutabletuples on the other hand are immutable objects. This means that lists can be modified, appended or sliced on-the-go but tuples remain constant and cannot be modified in any manner. You can run the following example on Python IDLE to confirm the difference:

my_tuple = ('sara', 6, 5, 0.97)
my_list = ['sara', 6, 5, 0.97]

print(my_tuple[0])     # output => 'sara'
print(my_list[0])     # output => 'sara'

my_tuple[0] = 'ansh'    # modifying tuple => throws an error
my_list[0] = 'ansh'    # modifying list => list modified

print(my_tuple[0])     # output => 'sara'
print(my_list[0])     <em style="-webkit-font-smoothing:subpixel-antialiased; border:0px; box-sizing:bor

Related questions

0 votes
    What is the difference between tuples and lists in Python?...
asked Nov 23, 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
    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
    Is there a nicer way to write the following function fs' with functors or applicatives? fncnB = (* ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 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
0 votes
    What is the difference between Python Arrays and lists?...
asked Dec 7, 2020 in Technology 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 the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 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
    I have two tuples -- TypeSets modeled as tuples, and thus guaranteed to contain each type at maximum once ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    The result which operation contains all pairs of tuples from the two relations, regardless of whether their ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What is the key difference between mobile device testing and mobile application testing?...
asked Feb 9, 2021 in Technology by JackTerrance
...