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
There's a huge difference.
__contains__
(in
operator) a lot more efficient for sets than lists.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
.
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.
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"}
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"]
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
Set represents a collection of distinct elements. In python, sets are mainly used for two reasons (Book: Data Science from Scratch, Joel Gruce):
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.
To find a distinct items in a collections. Programmers use sets much less frequently than dicts and lists.
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