Answer to Question #133300 in Python for hj

Question #133300
What is the difference between list and tuples in Python?
1
Expert's answer
2020-09-15T16:46:14-0400
  • Syntax.
a=(1,2,3) #tuple
a=[1,2,3]  #list
  • Lists are mutable objects, tuples are immutable. Length of list can be changed, length of tuple - not. As a consequence, there are more built-in functions for lists in comparison with that for tuples.
a = (1,2,3)
a[0]=10  # -> this line will cause an error

However, you can modify mutable objects inside of immutable ones, like

>>> b=(1,[1,2],3) 
>>> b[1].append(42) 
(1,[1,2,42],3)
  • All empty tuples are the same empty tuple, all empty lists are different lists.
  • Having the same information stored in tuple and in list, list will require more memory.
a = [1,2,3,4,5,6,7,8,9,0] #120 bytes
b = (1,2,3,4,5,6,7,8,9,0)  #104 bytes
  • Operations with tuples are a bit faster than operations with lists.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS