set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an iterable (like a list, tuple or dictionary) as input, removing duplicates automatically. Sets support various operations such as union, intersection and difference, making them useful for mathematical computations and data processing.
Example:
Python
a = set([1, 2, 3, 4, 2, 3])
print(a)
Syntax:
set(iterable)
here, iterable can be anything like list, tuple, dictionary or range. If no argument is provided it will create an empty set.
1. Creating an empty set
In Python, an empty set can be created using the set()
function, as curly braces {}
create an empty dictionary instead. An empty set is useful for storing unique elements dynamically. Since sets are mutable, elements can be added later using add()
or update()
. The type()
function can be used to verify the data type.
Python
a = set()
print(a)
print(type(a))
Outputset()
<class 'set'>
It will return the empty set.
2. set with list
A list can be converted into a set using set
()
. This removes duplicate elements and stores only unique values. Since sets are unordered, the order of elements in the original list may not be preserved. This method is useful for filtering out duplicate values from a list.
Python
a = [1, 2, 3, 4, 2, 3, 5]
b = set(a)
print(b)
List is converted into set.
3. set with tuple()
Tuples, like lists, can be converted into sets using the set() function. Since sets only store unique elements, duplicates from the tuple are removed. This is useful when needing a unique collection of immutable values. The resulting set is unordered and does not retain the tuple's order.
Python
tup = (1, 2, 3, 4, 2, 3, 5)
a = set(tup)
print(a)
tuple is converted into a set.
4. set with range()
A set can be created using the range() function, which generates a sequence of numbers. When passed to set(), it produces a set containing the unique numbers in the specified range. This method is useful for quickly generating sets of consecutive numbers.
Python
a = set(range(0, 11))
print(a)
Output{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
range function is used to create set.
5. set with dictionary
When a dictionary is converted into a set using set()
, only the keys are stored in the set. The values are ignored unless explicitly extracted and converted. This method is useful when working with unique keys from a dictionary.
Python
d = {'a': 1, 'b': 2, 'c': 3}
a = set(d)
print(a)
Output order may vary.
Similar Reads
sum() function in Python The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict
3 min read
type() function in Python The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re
5 min read
Python str() function The str() function in Python is an in-built function that takes an object as input and returns its string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Letâs take a simple example to converting an Intege
3 min read
Python print() function The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G
2 min read
Union() function in Python Union() method in Python is an inbuilt function provided by the set data type. It is used to combine multiple sets into a single set, containing all unique elements from the given sets. It ensures that no duplicate values exist in the final set.Python Set UnionThe symbol for denoting union of sets i
3 min read
Python Functions Python Functions is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it ov
11 min read
Python range() function The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops.ExampleIn the given example, we are printing the number from 0 to 4.Pythonfor i in range(5): print(i, end=" ") print()Output:0 1
7 min read
Operator Functions in Python | Set 1 Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module "operator". Some of the basic functions are covered in this article. 1. add(a, b) :- This function returns addition of the given arguments. Operation - a + b. 2. sub(a, b) :- This func
5 min read
Python Set discard() Function Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. Python3 my_set = {1, 2, 3, 4, 5} my_set.discar
3 min read