What is the difference between sets and lists in Python?

前端 未结 7 663
终归单人心
终归单人心 2020-11-29 19:14

Is the only difference between sets and lists in Python the fact that you can use the union, intersect, difference, symmetric difference functions to compare two sets? Why c

7条回答
  •  一整个雨季
    2020-11-29 19:59

    Set represents a collection of distinct elements. In python, sets are mainly used for two reasons (Book: Data Science from Scratch, Joel Gruce):

    1. For faster operation: in is a very fast operation on sets. If we have a large collection of elements and if we wish to perform membership test, in that case it is appropriate to use set instead of a list.

    2. To find a distinct items in a collections. Programmers use sets much less frequently than dicts and lists.

提交回复
热议问题