Skip to content
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

Open
364499361 opened this issue Nov 26, 2023 Discussed in #243 · 1 comment
Open

What is the difference between tuples and lists? #251

364499361 opened this issue Nov 26, 2023 Discussed in #243 · 1 comment

Comments

@364499361
Copy link

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?

@ysgdw
Copy link

ysgdw commented Nov 26, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants