What is the difference between sets and lists in Python?

前端 未结 7 658
终归单人心
终归单人心 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:54

    Set

    A set is a collection which is unordered and unindexed, and doesnt allow duplicates. In Python, sets are written with curly brackets.

    # example set
    newset = {"one", "two", "three"}
    
    • You cannot access items in a set by referring to an index
    • Sets are mutable
    • They are useful for checking for duplicates

    List

    A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

    # example list
    newlist =["one", "two", "three"]
    
    • You access the list items by referring to the index number
    • Lists are mutable.

提交回复
热议问题