Set, a term in mathematics for a sequence consisting of distinct language is also extended in its language by Python and can easily be made using set().
set() method is used to convert any of the iterable to sequence of iterable elements with dintinct elements, commonly called Set.
Syntax : set(iterable)
Parameters : Any iterable sequence like list, tuple or dictionary.
Returns : An empty set if no element is passed. Non-repeating element iterable modified as passed as argument.
Don’t worry if you get an unordered list from the set. Sets are unordered. Use sorted(set(sampleList)) to get it sorted
Code #1 : Demonstrating set() with list and tuple
# Python3 code to demonstrate the # working of set() on list and tuple # initializing list lis1 = [ 3, 4, 1, 4, 5 ] # initializing tuple tup1 = (3, 4, 1, 4, 5) # Printing iterables before conversion print("The list before conversion is : " + str(lis1)) print("The tuple before conversion is : " + str(tup1)) # Iterables after conversion are # notice distinct and elements print("The list after conversion is : " + str(set(lis1))) print("The tuple after conversion is : " + str(set(tup1))) |
Output:
The list before conversion is : [3, 4, 1, 4, 5]
The tuple before conversion is : (3, 4, 1, 4, 5)
The list after conversion is : {1, 3, 4, 5}
The tuple after conversion is : {1, 3, 4, 5}
- No parameters are passed to create the empty set
- Dictionary can also be created using set, but only keys remain after conversion, values are lost.
Code #2: Demonstration of working of set on dictionary
# Python3 code to demonstrate the # working of set() on dictionary # initializing list dic1 = { 4 : 'geeks', 1 : 'for', 3 : 'geeks' } # Printing dictionary before conversion # internaly sorted print("Dictionary before conversion is : " + str(dic1)) # Dictionary after conversion are # notice lost keys print("Dictionary afer conversion is : " + str(set(dic1))) |
Output:
Dictionary before conversion is : {1: 'for', 3: 'geeks', 4: 'geeks'}
Dictionary afer conversion is : {1, 3, 4}
Recommended Posts:
- Python | Set 4 (Dictionary, Keywords in Python)
- class method vs static method in Python
- Difference between Method Overloading and Method Overriding in Python
- Set Window Icon Text in PyQt5 | setWindowIconText() method
- PyQt5 – How to set tooltip duration for Label | setToolTipDuration method
- PyQt5 – How to set minimum size of window | setMinimumSize method
- PyQt5 – Set minimum window size | setMinimumWidth and setMinimumHeight method
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Generate all permutation of a set in Python
- Interesting facts about strings in Python | Set 1
- Interesting facts about strings in Python | Set 2 (Slicing)
- Creating a Proxy Webserver in Python | Set 1
- Function Decorators in Python | Set 1 (Introduction)
- Creating a Proxy Webserver in Python | Set 2
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Regular Expressions in Python | Set 2 (Search, Match and Find All)
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : yashthakkar1173

