Difference between list and set in Python







  • What is the difference between set and list in Python

    Sets can't contain duplicates while lists can
    Sets are unordered while lists are ordered sequences
    In order to find an element in a set, a hash lookup is used (which is why sets are unordered). This makes __contains__ (in operator) a lot more efficient for sets than lists. Elements of a list can be found by index as lists are ordered collections
    Sets can only contain hashable items while lists can contain any objects
  • python bootcamp 2021
datai analytics