What is the difference between sets and lists in Python?

PythonListSet

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

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.

Solution 6 - Python

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

Solution 7 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1588869View Question on Stackoverflow
Solution 1 - PythonmgilsonView Answer on Stackoverflow
Solution 2 - PythonAshwini ChaudharyView Answer on Stackoverflow
Solution 3 - PythonP.singhView Answer on Stackoverflow
Solution 4 - PythonBlackBeardView Answer on Stackoverflow
Solution 5 - PythonSayali SonawaneView Answer on Stackoverflow
Solution 6 - PythonSoft_CoderView Answer on Stackoverflow
Solution 7 - Pythongurpal mannView Answer on Stackoverflow