59

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 can't these functions simply be applied to lists? In what situations are sets more useful than lists?

DSM
  • 342,061
  • 65
  • 592
  • 494
user1588869
  • 641
  • 1
  • 6
  • 8
  • 1
    Sets, by definition, don't include duplicate values; I assume that Python, like other languages, will allow duplicate entries in a list. – Anthony Grist Sep 10 '12 at 15:11
  • 2
    This is laid out pretty well in the documentation. [Sets](http://docs.python.org/tutorial/datastructures.html#sets) and [Lists](http://docs.python.org/tutorial/datastructures.html#more-on-lists) – Scott Bartell Sep 10 '12 at 15:35

7 Answers7

92

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.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 4
    Also, sets can contain only objects that are hashable; lists can contain any kind of object. – kindall Sep 10 '12 at 15:47
  • @kindall -- That's implicit in the explanation for #3, but I've added it as #4 to make it more explicit. Thanks for the comment. – mgilson Sep 10 '12 at 15:50
14

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.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
7

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.
benzkji
  • 1,718
  • 20
  • 41
P.singh
  • 71
  • 1
  • 2
5

Some more differences are:

  1. List can be 2-D whereas a set can't.
  2. As list are ordered (IE. have serial number) list are comparatively slow to execute whereas sets are fast.
  3. List in python is like Array of java or c.
  4. Printing a set almost always provide different sequence of output.
  5. Set uses hash function to find an element whereas list is an array. Hence finding element in Set is faster than in list.
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
5

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.

Sayali Sonawane
  • 12,289
  • 5
  • 46
  • 47
3

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

Community
  • 1
  • 1
Soft_Coder
  • 184
  • 3
  • 6
1

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