• Register
1 vote
367 views

Problem:

I have been doing programming for a while and I am using python for this. So my question is 

What is an advantage of using a tuple rather than a list?

Thank you 

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

Advantages and disadvantages of tuple and list;

You can't add elements to a tuple. Tuples have no append or extend method.You can't remove elements from a tuple. 
Tuples have no remove or pop method.You can find elements in a tuple, since this doesn’t change the tuple.You can also use the in operator to check if an element exists in the tuple.
Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate through it, use a tuple instead of a list.It makes your code safer if you write-protect data that does not need to be changed. 
Using a tuple instead of a list is like having an implied assert statement that this data is constant, and that special thought (and a specific function) is required to override that.Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples). Lists can never be used as dictionary keys, because lists are not immutable.

Below I have given the difference between list and tuple

Literal

someTuple = (1,2)
someList  = [1,2] 
Size

x = tuple(range(1000))
y = list(range(1000))

x.__sizeof__() # 8024
y.__sizeof__() # 9088
Due to the smaller size of a tuple operation, it becomes a bit faster, but not that much to mention about until you have a huge number of elements.

Permitted operations

y    = [1,2]   
y[0] = 3       # [3, 2]

x    = (1,2)
x[0] = 3       # Error

That also means that you can't delete an element or sort a tuple. However, you could add a new element to both list and tuple with the only difference that since the tuple is immutable, you are not really adding an element but you are creating a new tuple, so the id of will change

x     = (1,2)
y     = [1,2]  

id(x)          # 140230916716520
id(y)          # 748527696

x   += (3,)    # (1, 2, 3)
y   += [3]     # [1, 2, 3]

id(x)          # 140230916878160
id(y)          # 748527696
Usage

As a list is mutable, it can't be used as a key in a dictionary, whereas a tuple can be used.

x    = (1,2)
y    = [1,2] 

z = {x: 1}     # OK
z = {y: 1}     # Error

Hope you understand this if you can not ask me anything you want to know about this

11 6 4
34,950 points

Related questions

0 votes
1 answer 16 views
16 views
Problem In Python, when should you use lists and when tuples? Sometimes you don't have a choice, for example, if you have
asked Mar 10 neeraj 10.5k points
1 vote
1 answer 58 views
58 views
Problem: “indexerror: tuple index out of range” what does it mean? Why I am getting this error? Please shed some light on this if possible. Gratitude.
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 39 views
39 views
Problem: Please consider the following code: i = [1, 2, 3, 5, 8, 13] j = [] k = 0 for l in i: j[k] = l k += 1 print j The output (Python 2.6.6 on Win 7 32-bit) is: > Traceback (most recent call last): > j[k] = l IndexError: list assignment index out of range I guess it's something simple I don't understand. Can someone clear it up?
asked Jan 10 Mashhoodch 13k points
0 votes
1 answer 45 views
45 views
I wrote a code in python : l = [8, 5, 4] total = l[0] + l[1] print (total) def count(a): total = 0 for i in a: total+= a[i] return total print (count(n)) but it has some error: File "main.py", line 11 return total ^ SyntaxError: 'return' outside function What is this error causing for?
asked Mar 15, 2020 LizzyM 6.1k points
0 votes
1 answer 14 views
14 views
Problem: How to solve this error: How do I list all files of a directory in python ?
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
1 answer 7 views
7 views
Problem: how to I Solve this: python add all elements of a list HELP! I need help understanding this: python add all elements of a list Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 11 views
11 views
Problem: I have a list of arbitrary length and I need to split it into chunks of the same size and process it. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list is full, add it to the first ... of any length, for example using generators. I've searched for something useful in itertoolsbut couldn't find anything useful. I may have missed this.
asked Dec 23, 2020 sasha 16.2k points
0 votes
2 answers 34 views
34 views
How can I list all files of a directory in Python and add them to a list ?
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 39 views
39 views
Problem: I am new to python. Can anyone help me out? How to delete a list in python? Thanks!!
asked Mar 5 Samiur1 38.4k points
0 votes
1 answer 24 views
24 views
Problem: how to I Solve this: product of all elements in list python HELP! I need help understanding this: product of all elements in list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 8 Mashhoodch 13k points