+ 1

Why is it faster to check if an element is part of a set than a list?

The tutorial says that the reason is the way sets are stored. But I want a more detailed explanation.

27th Jul 2020, 10:41 AM
Punyasloka Sahoo
Punyasloka Sahoo - avatar
1 Odpowiedź
+ 2
Because set contains only uniques while list can contain duplicate. So it is faster to search through set. list=[1,2,3,2] a=set(list) print(a) Output: {1,2,3} or {2,1,3} as sets are not indexed.
27th Jul 2020, 11:37 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar