- Understanding the set() function
- 5 Answers 5
- Python Set Function
- How Sets Work in Python?
- Implementing Set Function in Python
- 1. Add Method
- 2. Remove Method
- 3. Discard Method
- 4. Clear Method
- 5. Copy Method
- 6. Pop Method
- 7. Update Method
- Python Set Operation
- 1. Union
- 2. Intersection
- 3. Difference
- Conclusion
- Recommended Articles
Understanding the set() function
In python, set() is an unordered collection with no duplicate elements. However, I am not able to understand how it generates the output. For example, consider the following:
>>> x = [1, 1, 2, 2, 2, 2, 2, 3, 3] >>> set(x) set([1, 2, 3]) >>> y = [1, 1, 6, 6, 6, 6, 6, 8, 8] >>> set(y) set([8, 1, 6]) >>> z = [1, 1, 6, 6, 6, 6, 6, 7, 7] >>> set(z) set([1, 6, 7])
5 Answers 5
Sets are unordered, as you say. Even though one way to implement sets is using a tree, they can also be implemented using a hash table (meaning getting the keys in sorted order may not be that trivial).
If you’d like to sort them, you can simply perform:
which will produce a sorted list containing the set’s elements. (Not a set. Again, sets are unordered.)
Otherwise, the only thing guaranteed by set is that it makes the elements unique (nothing will be there more than once).
Note: Sets in general can be implemented using trees. set in Python cannot (meaningfully), as it’s guarantees that they require the items to be hashable and don’t require them to be comparable.
@delnan — And just to be abundantly clear, by comparable you mean rich comparisons ( < , >). In order for an object to be «hashable» it must implement __eq__ .
@mgilson: Tangentially, in 2.x if you override __eq__ and need to use instances in a set or as dict keys, then you should also override __hash__ such that equal objects hash the same. Or define __hash__ = None to make it unhashable. In 3.x, if you override __eq__ you are required to override __hash__ if you want the type to be hashable.
@eryksun — Yes. I suppose I should have said that it also must implement __eq__ (in addition to __hash__ ).
@mgilson: For 2.x I should have said ‘implement’ __eq__ , not ‘override’ it, since only 3.x has default rich comparison methods. However, there is a default object.__hash__ in 2.x. It’s OK to override this without implementing __eq__ , just not vice versa.
As an unordered collection type, set([8, 1, 6]) is equivalent to set([1, 6, 8]) .
While it might be nicer to display the set contents in sorted order, that would make the repr() call more expensive.
Internally, the set type is implemented using a hash table: a hash function is used to separate items into a number of buckets to reduce the number of equality operations needed to check if an item is part of the set.
To produce the repr() output it just outputs the items from each bucket in turn, which is unlikely to be the sorted order.
As +Volatility and yourself pointed out, sets are unordered. If you need the elements to be in order, just call sorted on the set:
>>> y = [1, 1, 6, 6, 6, 6, 6, 8, 8] >>> sorted(set(y)) [1, 6, 8]
Python’s sets (and dictionaries) will iterate and print out in some order, but exactly what that order will be is arbitrary, and not guaranteed to remain the same after additions and removals.
Here’s an example of a set changing order after a lot of values are added and then removed:
>>> s = set([1,6,8]) >>> print(s) >>> s.update(range(10,100000)) >>> for v in range(10, 100000): s.remove(v) >>> print(s)
This is implementation dependent though, and so you should not rely upon it.
Python Set Function
Sets are the data types in python that are used to store the collection of data. These data can be of any type. It can be an integer, float, string, or both. Sets are similar to List and Tuples. In Sets, values are stored inside the curly brackets. Values stored in the sets cannot be duplicated. They are unique. Even if we define any duplicate value, python will ignore it while executing the sets. We have various inbuilt set functions that can be used to perform various operations on Sets.
Web development, programming languages, Software testing & others
How Sets Work in Python?
Commas separate elements that are stored inside the curly brackets. In the list, we can have another list inside a list or a nested list. But in Sets, we cannot store a list or set or dictionary as an element of the set. The values stored in the sets are mutable means they can be changed.
If we want to search for any value in the set using index value, it will give an error. Sets are unordered means values don’t have fix position. So we cannot perform any kind of operation using an index. We cannot create an empty set by just initializing empty curly brackets; it will create a dictionary. To create an empty set, we have to use the set function. If you want to list functions supported by sets, you can use the ‘dir’ method and pass the set inside it. It will return the list of methods supported by sets.
An example of a python set function is given below:
As I mentioned earlier, the doesn’t take duplicate values.
5 is appearing 2 times, but python will consider the value which appeared first and skips the next value while executing the set.
Suppose you have a List and consists of many duplicates values, then we can use sets to remove those duplicate values.
Implementing Set Function in Python
Methods to implement a set function in python is given below:
1. Add Method
This method is used to add the element to the set. The element will be added to the unspecific location as it is unordered.
sample = sample.add(7) print(sample)
The sample added cannot be added again in the set.
2. Remove Method
This method is used to remove any value from the list as there is no indexing, so we have to pass the value that we want to remove from the set.
sample = sample.remove(5) print(sample)
3. Discard Method
This method is also used to remove an element from the set, but the remove method will generate an error if the passed value does not exist in the set. This method is used when we don’t know the value that we are passing exist or not. This method won’t return any error.
sample = sample.discard(5) print(sample)
4. Clear Method
This method is used to remove all the elements from the set.
sample = sample.clear() print(sample)
5. Copy Method
This method is used to make a copy of the Set.
sample = new_sample = sample.copy() print(new_sample)
6. Pop Method
This method is used to remove an element from the set. If we don’t specify any element in the pop method, then by default, it will remove the first element of the set.
sample = sample = sample.pop() print(sample)
7. Update Method
We can use this method to add multiple values to a set, we can also pass new sets or list or both, and it will be added to the set.
sample = sample.update([6,7,8]) print(sample)
sample = sample.update([6,7,8]) new_set = sample.update([1,6,7,8],new_set) print(sample)
So as you can first, we have added that was having duplicate values that already existed inside the set, and we have passed a new set that also had duplicate value. But when we print to set, it discards all duplicate values.
Python Set Operation
We can perform mathematical operations like Union, Intersection, Difference on sets. This can be done in two ways using methods or operators.
1. Union
This function used to merge the elements of two sets into one set.
sample = new_sample = sample_union = (sample | new_sample) print(sample_union)
sample = new_sample = sample_union = sample.union(new_sample) print(sample_union)
2. Intersection
This method is used to find out the common elements from two sets.
sample = new_sample = sample_intersection = (sample & new_sample) print(sample_intersection)
sample = new_sample = sample_intersection = sample.intersection(new_sample) print(sample_intersection)
3. Difference
This method is used to find the difference of the element from one set to the second set. It means it will return the element that doesn’t exist in the set first as compared to the set second.
sample = new_sample = sample_difference = (sample - new_sample) print(sample_difference)
sample = new_sample = sample_difference = sample.difference(new_sample) print(sample_difference)
Conclusion
So as we have seen, Set is a very useful data type in Python. It is very similar to the list, the only difference is brackets, and it doesn’t take duplicates values. We can use sets to store the data where the data has to be unique is very important, and ordering of the data is not important as they are not unordered.
Recommended Articles
This is a guide to Python Set Function. Here we discuss the basic concept, methods to implement a set function in python, and different examples and code implementation. You may also look at the following articles to learn more –