What is the difference between sets and lists in Python?

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

    There's a huge difference.

    1. Sets can't contain duplicates
    2. Sets are unordered
    3. 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.
    4. Sets can only contain hashable items (see #3). If you try: set(([1],[2])) you'll get a TypeError.

    In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order.

    Also note that if you don't care about order, etc, you can use

    new_set = myset.intersection(mylist)
    

    to get the intersection between a set and a list.

    0 讨论(0)
  • 2020-11-29 19:53

    sets — Unordered collections of unique elements

    lists - ordered collections of elements

    sets allows you to do operations such as intersection, union, difference, and symmetric difference, i.e operations of math's set theory. Sets doesn't allow indexing and are implemented on hash tables.

    lists are really variable-length arrays, not Lisp-style linked lists. In lists the elements are accessed by indices.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-11-29 19:57

    Actually there are four collection data types of in python:

    List is a collection which is ordered and changeable. Allows duplicate members.

    Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

    Set is a collection which is unordered and unindexed. No duplicate members.

    Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.

    You can access a list item by referring to its index. however, in sets, you need to loop through the set items in order to access it.

    source: https://www.w3schools.com/python/python_sets.asp

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 20:03

    Difference Between Sets and Lists Here we will discuss the difference between Sets and List in Python.

    Lists 1) Lists save elements in the order they are inserted. 2) Lists support indexing. 3) We can change the value of the element stored in the lists. 4) Lists can store duplicate values. 5) Lists are declared using square brackets. 6) Example: A = [1, 2, 3, 4, 5, 1, 2, 3]

    Sets 1) Sets do not save elements in the order they are inserted. 2) Sets do not support indexing. 3) We cannot change the value of the element stored in the sets. 4) Sets cannot store duplicate values. 5) Sets are declared using curly brackets. 6) Example: A = {1, 2, 3, 4, 5}

    Learn more on Sets with Example on the link given below https://tutorialsimpact.com/python/sets-in-python

    0 讨论(0)
提交回复
热议问题