Python Collections
A Set in Python is an unordered collection of data types that is iterable, changeable, and does not contain duplicate entries. The order of the items in a set is unknown, yet it may include several elements. The main benefit of utilising a set over a list is that it provides a highly efficient way for determining if a certain member is in the set.
Building a Set Sets may be constructed by using the built-in set() method with an iterable object or a sequence enclosed in curly braces and separated by a ‘comma’.
Since a set is changeable, it cannot have mutable items like a list or dictionary.
# Python program to demonstrate # Creation of Set in Python # Creating a Set set1 = set () print ( "Initial blank Set: " ) print (set1) # Creating a Set with # the use of a String set1 = set ( "GeeksForGeeks" ) print ( "\nSet with the use of String: " ) print (set1) # Creating a Set with # the use of Constructor # (Using object to Store String) String = 'GeeksForGeeks' set1 = set (String) print ( "\nSet with the use of an Object: " ) print (set1) # Creating a Set with # the use of a List set1 = set ([ "Geeks" , "For" , "Geeks" ]) print ( "\nSet with the use of List: " ) print (set1) |
Output
Initial blank Set:
set()
Set with the use of String:
{‘r’, ‘s’, ‘o’, ‘F’, ‘G’, ‘k’, ‘e’}
Set with the use of an Object:
{‘r’, ‘s’, ‘o’, ‘F’, ‘G’, ‘k’, ‘e’}
Set with the use of List:
{‘For’, ‘Geeks’}
O(n) is the time complexity, where n is the length of the input string or list.
O(n), where n is the length of the input string or list, since the size of the set formed is dependent on the size of the input.
A set has only unique components, however several duplicate values may be given while creating the set. The order of items in a set is undefined and cannot be changed. A set’s elements do not have to be of the same type; other mixed-up data type values may also be provided to the set.
# Creating a Set with # a List of Numbers # (Having duplicate values) set1 = set ([ 1 , 2 , 4 , 4 , 3 , 3 , 3 , 6 , 5 ]) print ( "\nSet with the use of Numbers: " ) print (set1) # Creating a Set with # a mixed type of values # (Having numbers and strings) set1 = set ([ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]) print ( "\nSet with the use of Mixed Values" ) print (set1) |
Output
Set with the use of Numbers:
{1, 2, 3, 4, 5, 6}
Set with the use of Mixed Values
{1, 2, 4, 6, ‘Geeks’, ‘For’}
Building a set using a different technique
# Another Method to create sets in Python3 # Set containing numbers my_set = { 1 , 2 , 3 } print (my_set) # This code is contributed by sarajadhav12052009 |
Output
{1, 2, 3}
Using the add() function to add Items to a Set
The built-in add() method may be used to add elements to the Set. The add() function can only add one element to the set at a time; loops are used to add numerous pieces at once using the add() method.
Note that lists cannot be added as elements to a set because they are not hashable, however tuples may be added since they are immutable and hence hashable.
# Python program to demonstrate # Addition of elements in a Set # Creating a Set set1 = set () print ( "Initial blank Set: " ) print (set1) # Adding element and tuple to the Set set1.add( 8 ) set1.add( 9 ) set1.add(( 6 , 7 )) print ( "\nSet after Addition of Three elements: " ) print (set1) # Adding elements to the Set # using Iterator for i in range ( 1 , 6 ): set1.add(i) print ( "\nSet after Addition of elements from 1-5: " ) print (set1) |
Output
Initial blank Set:
set()
Set after Addition of Three elements:
{8, 9, (6, 7)}
Set after Addition of elements from 1-5:
{1, 2, 3, (6, 7), 4, 5, 8, 9}
Making use of the update() function
The Update() function is used to add two or more components. The update() function takes as inputs lists, strings, tuples, and other collections. Duplicate items are avoided in all of these scenarios.
# Python program to demonstrate # Addition of elements in a Set # Addition of elements to the Set # using Update function set1 = set ([ 4 , 5 , ( 6 , 7 )]) set1.update([ 10 , 11 ]) print ( "\nSet after Addition of elements using Update: " ) print (set1) |
Output
Set after Addition of elements using Update:
{4, 5, (6, 7), 10, 11}
Obtaining Access to a Set
Set items cannot be retrieved using an index because sets are unordered and the elements have no index. But, you may use a for loop to cycle over the set elements, or the in keyword to query whether a specific value is contained in a set.
# Python program to demonstrate # Accessing of elements in a set # Creating a set set1 = set ([ "Geeks" , "For" , "Geeks" ]) print ( "\nInitial set" ) print (set1) # Accessing element using # for loop print ( "\nElements of set: " ) for i in set1: print (i, end = " " ) # Checking the element # using in keyword print ( "Geeks" in set1) |
Output
Initial set
{‘Geeks’, ‘For’}
Elements of set:
Geeks For True
Element removal from the Set
Using the delete() or discard() methods:
Items may be deleted from the Set using the built-in remove() method, however if the element does not exist in the set, a KeyError is thrown. Use discard() to delete entries from a set without raising a KeyError; if the element does not exist in the set, it stays unaffected.
# Python program to demonstrate # Deletion of elements in a Set # Creating a Set set1 = set ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]) print ( "Initial Set: " ) print (set1) # Removing elements from Set # using Remove() method set1.remove( 5 ) set1.remove( 6 ) print ( "\nSet after Removal of two elements: " ) print (set1) # Removing elements from Set # using Discard() method set1.discard( 8 ) set1.discard( 9 ) print ( "\nSet after Discarding two elements: " ) print (set1) # Removing elements from Set # using iterator method for i in range ( 1 , 5 ): set1.remove(i) print ( "\nSet after Removing a range of elements: " ) print (set1) |
Output
Initial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Set after Removal of two elements:
{1, 2, 3, 4, 7, 8, 9, 10, 11, 12}
Set after Discarding two elements:
{1, 2, 3, 4, 7, 10, 11, 12}
Set after Removing a range of elements:
{7, 10, 11, 12}
Using the pop() function:
The Pop() method may also be used to remove and return one element from a set, however it only removes the set’s final element.
There is no way to know which element gets popped by using the pop() method if the set is unordered.
# Python program to demonstrate # Deletion of elements in a Set # Creating a Set set1 = set ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]) print ( "Initial Set: " ) print (set1) # Removing element from the # Set using the pop() method set1.pop() print ( "\nSet after popping an element: " ) print (set1) |
Output
Initial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Set after popping an element:
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Using the function clear():
The clear() method is used to delete all of the items from the set.
#Creating a set set1 = set ([ 1 , 2 , 3 , 4 , 5 ]) print ( "\n Initial set: " ) print (set1) # Removing all the elements from # Set using clear() method set1.clear() print ( "\nSet after clearing all the elements: " ) print (set1) |
Output
Initial set:
{1, 2, 3, 4, 5}
Set after clearing all the elements:
set()
In Python, frozen sets are immutable objects that only support methods and operators that yield a result without changing the frozen set or sets to which they are applied. Although parts of a set may be changed at any moment, elements of a frozen set do not change once they are created.
It returns an empty frozenset if no arguments are supplied.
# Python program to demonstrate # working of a FrozenSet # Creating a Set String = ( 'G' , 'e' , 'e' , 'k' , 's' , 'F' , 'o' , 'r' ) Fset1 = frozenset (String) print ( "The FrozenSet is: " ) print (Fset1) # To print Empty Frozen Set # No parameter is passed print ( "\nEmpty FrozenSet: " ) print ( frozenset ()) |
Output
The FrozenSet is:
frozenset({‘F’, ‘s’, ‘o’, ‘G’, ‘r’, ‘e’, ‘k’})
Empty FrozenSet:
frozenset()
Object typecasting into sets
# Creating a Set with # a List of Numbers # (Having duplicate values) set1 = set ([ 1 , 2 , 4 , 4 , 3 , 3 , 3 , 6 , 5 ]) print ( "\nSet with the use of Numbers: " ) print (set1) # Creating a Set with # a mixed type of values # (Having numbers and strings) set1 = set ([ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]) print ( "\nSet with the use of Mixed Values" ) print (set1) |
0
My list should be output as a set: 1, 2, 3, 4, 5, 6.
my str as a collection: ‘G,’ ‘f,’ ‘r,’ ‘e,’ ‘k,’ ‘o,”s’
my dict as a collection:
{1, 2, 3}
Example: All functions are being implemented:
# Creating a Set with # a List of Numbers # (Having duplicate values) set1 = set ([ 1 , 2 , 4 , 4 , 3 , 3 , 3 , 6 , 5 ]) print ( "\nSet with the use of Numbers: " ) print (set1) # Creating a Set with # a mixed type of values # (Having numbers and strings) set1 = set ([ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]) print ( "\nSet with the use of Mixed Values" ) print (set1) |
1
1, 2, 3, 4, 5 Output 1, 2, 3, 4, 5, 6
{1, 2, 4, 5}
set() 1, 2, 3, 4, 5, 6 @@@2
{1, 2, 3}
{1, 2, 3, 6, 7, 8}
True
True
Advantages:
Disadvantages:
Generally, sets may be a helpful data structure in Python, particularly for deleting duplicates and doing quick membership tests. Nevertheless, their lack of ordering and restricted functionality might make them less adaptable than lists or dictionaries, so when determining which data structure to utilise in your Python application, it is critical to carefully analyse the pros and downsides of utilising sets.
Table Methods@@@0