-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the difference between tuples and lists? #251
Comments
Difference: variability: this is the most significant difference. Lists are mutable, that is, you can add, remove, or modify elements after the list has been created. Tuples are immutable, and once created, elements cannot be added, removed, or modified. Python # list is mutable my_list = [1, 2, 3] my_list. Append (# 4) you can add elements my_list [0] = 0 # # can modify element tuple is immutable my_tuple = (1, 2, 3) my_tuple [0] = 0 # would trigger a TypeError grammar said: list using square brackets [], said the yuan group said the parentheses (). Python my_list = [1, 2, 3] my_tuple = (1, 2, 3) performance: as the tuple is immutable, their performance may be slightly better than the list. In some cases, tuples may be better used as keys than lists, such as in dictionaries. Usage scenarios: normally, when you have a set of data that will not change, you can use a tuple. When you need a set of data that you can modify at will, you can use lists. |
What is the difference between tuples and lists?In some cases, tuples are faster than lists.why?Can you explain the optimization method of tuples in detail?
The text was updated successfully, but these errors were encountered: