5 Risposte
+ 6
Lists are mutable(items can be changed after a list has been created).
Tuples are immutable(items can NOT be changed after a tuple has been created).
Example:
# list
my_list = ["Python", "C", "Java"]
my_list[1] = "C++" # valid
# tuple
my_tuple = ("Python", "C", "Java")
my_tuple[2] = "Kotlin" # TypeError: "tuple" object does not support item assignment
+ 5
Tanvi Saxena ,
besides of what Afnan Irtesum Chowdhury mentioned, tuples are slightly faster in searching and lookup. this is due to how tuples and lists are stored in memory.
> tuples uses a continuous memory block.
> lists store pointers to list objects.
+ 3
if you're going to modify it, use list. otherwise, use tuples. tuples are more memory efficient. they use less memory and are faster to access than lists.
+ 3
Brian James ,
read my post above...
0
While lists are mutable, tuples are inmutable. Besides, lists in Python are created with square brackets [], and tuples with normal brackets (). However, the concept of both is the same, it's like creating kind of a variable but with some elements inside it.
Both of them are ordered, and you can use indexation in order to access to determined elements in the list or tuple.
I hope that I'm answering your question, if you don't understand this at all, I'll try to answer it clearer if u answer to this message.
Have a good day!