Which is faster to access data: list, tuple, or set? #1
Unanswered
AbdulRehman393
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Tuples are generally faster than lists for accessing data because they are immutable.
Sets are optimized for membership test, so checking if an item exists is faster than in lists or tuples.
Lists are good for ordered data and indexing, but slower for membership checks.
Example:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_set = {1, 2, 3}
Access
print(my_list[0])
print(my_tuple[0])
print(1 in my_set)
All reactions