• Register
1 vote
242 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 20 views
20 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 9.1k points
0 votes
1 answer 8 views
8 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 8.7k points
0 votes
2 answers 21 views
21 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
1 vote
1 answer 39 views
39 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 12 views
12 views
Problem: The following error message is displayed: “Oops, try again. get_class_average([alice]) resulted in an error: ‘list’ object attribute ‘append’ is read-only” This is my code: lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0] }
asked Feb 18 Mashhoodch 9.1k points
1 vote
1 answer 1.4K views
1.4K views
Problem: I am a beginner in python programming language. I tried to create a program to calculate my students avarage results so I wrote this code porter = { "name": "Porter", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0] ... try again. getclassaverage([porter]) resulted in an error: 'list' object attribute 'append' is read-only" Can anyone help me out to solve this problem?
asked Apr 5, 2020 LizzyM 6.1k points
0 votes
1 answer 45 views
45 views
I am new to python programming language. I wrote a where I tried to pass a list of functions : def function(f. *args) for f in args print f I can not pass my list of function using this code It gives me error File "main.py", line 1 def function(f. *args) ^ ... : File "main.py", line 1 def function(*args) ^ SyntaxError: invalid syntax How can I solve this and how can I pass a list of function?
asked Mar 12, 2020 LizzyM 6.1k points
0 votes
1 answer 35 views
35 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 5 views
5 views
Problem: How can I find all the files in a directory having the extension .txt in python?The pathlib module was included in the standard library in python 3.4. But you can install back-ports of that module even on older Python versions (i.e. using conda or pip): pathlib and pathlib2.
asked Feb 19 Mashhoodch 9.1k points
0 votes
1 answer 64 views
64 views
Problem Hi Kodlogs, How do I list all files in a directory and its subdirectories in Python? Apparently, I am unable to do so.
asked Oct 28, 2020 hashq 1.1k points