- 1Python aiter() Function – Get Async Iterator in Python
- 2Python all() Function – Check If All Elements Are True
- 3Python anext() Function – Get Next Item from Async Iterator
- 4Python any() Function – Check if Any Element is True
- 5Python ascii() Function – Get ASCII Representation of Objects
- 6Python bin() Function – Convert Integer to Binary String
- 7Python bool() Function – Convert to Boolean
- 8Python breakpoint() Function – Add Debug Points in Your Code
- 9Python bytearray() Function – Create and Modify Binary Data
- 10Python bytes() Function – Create Immutable Byte Objects
- 11Python callable() Function – Check If Object Is Callable
- 12Python chr() Function – Get Character from Unicode Code Point
- 13Python classmethod() Function – Convert Method to Class Method
- 14Python compile() Function – Compile Source Code at Runtime
- 15Python complex() Function – Create Complex Numbers Easily
- 16Python delattr() Function – Delete Attribute from Object
- 17Python dict() Function – Create and Use Dictionaries Easily
- 18Python dir() Function – List Attributes of Objects
- 19Python divmod() Function – Quotient and Remainder in One Go
- 20Python enumerate() Function – Loop with Index Easily
- 21Python eval() Function – Evaluate Expressions from Strings
- 22Python exec() Function – Execute Dynamic Python Code
- 23Python filter() Function – Filter Items in a List
- 24Python float() Function – Convert to Floating Point Number
- 25Python format() Function – Format Strings Easily
- 26Python frozenset() Function – Create Immutable Sets
- 27Python getattr() Function – Access Object Attributes Dynamically
- 28Python globals() Function – Access Global Symbol Table
- 29Python hasattr() Function – Check If an Object Has an Attribute
- 30Python hash() Function – Generate Hash Values of Objects
- 31Python help() Function – Get Help on Python Functions, Classes, and Modules
- 32Python hex() Function – Convert Integer to Hexadecimal
- 33Python id() Function – Get Memory Address of an Object
- 34Python input() Function – Get User Input Easily
- 35Python int() Function – Convert to Integer
- 36Python isinstance() Function – Check Type of a Variable
- 37Python issubclass() Function – Check Class Inheritance
- 38Python iter() Function – Create an Iterator from Iterable
- 39Python len() Function – Get the Length of Strings, Lists, and More
- 40Python list() Function – Convert Data to a List
- 41Python locals() Function – Get Local Symbol Table as a Dictionary
- 42Python map() Function – Apply a Function to Each Element
- 43Python max() Function – Find the Largest Value
- 44Python memoryview() Function – Work With Binary Data Efficiently
- 45Python min() Function – Find the Smallest Value
- 46Python next() Function – Retrieve Next Item from an Iterator
- 47Python object() Function – Base Object Constructor Explained
- 48Python oct() Function – Convert Number to Octal
- 49Python open() Function – Read, Write, and Create Files Easily
- 50Python ord() Function – Get Unicode Code of a Character
- 51Python pow() Function – Raise a Number to a Power
- 52Python print() Function – Print Output to the Console
- 53Python property() Function – Create Managed Attributes
- 54Python range() Function – Generate Number Sequences Easily
- 55Python repr() Function – Get String Representation of an Object
- 56Python reversed() Function – Reverse Iterables Easily
- 57Python round() Function – Round Numbers to Nearest Integer or Decimal Places
- 58Python set() Function – Create a Set from Iterable
- 59Python setattr() Function – Dynamically Set Object Attributes
- 60Python slice() Function – Create Slice Objects for Lists and Strings
- 61Python sorted() Function – Sort Any Iterable Easily
- 62Python staticmethod() Function – Create Static Methods in Classes
- 63Python str() Function – Convert to String Easily
- 64Python sum() Function – Add Items of an Iterable
- 65Python super() Function – Call Parent Class Methods
- 66Python tuple() Function – Create a Tuple from Iterable
- 67Python type() Function – Get the Type of an Object
- 68Python vars() Function – View Object’s Attributes
- 69Python zip() Function – Combine Iterables Element-Wise
- 70Python __import__() Function – Dynamic Module Import
- 71Python str.format() – Format Strings Easily with Placeholders
- 72Python String format_map() – Format Strings with a Mapping
Python set() Function – Create a Set from Iterable
Python set()
Function
The set() function in Python is used to create a set object. A set is an unordered collection of unique items — meaning it automatically removes duplicates.
Syntax
set([iterable])
Parameters:
iterable
– (Optional) Any iterable such as list, tuple, string, or dictionary keys.
Returns:
- A new
set
object containing unique elements.
Example 1: Convert a List to a Set
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers)
{1, 2, 3, 4, 5}
Example 2: Create a Set from a String
text = "banana"
letters = set(text)
print(letters)
{'b', 'a', 'n'}
Note: The order may vary because sets are unordered.
Example 3: Empty Set
empty = set()
print(empty)
set()
Warning: Using {}
creates an empty dict
, not a set
.
Use Cases of set()
- Remove duplicates from a list
- Check for unique items
- Perform set operations like union, intersection, difference
Example 4: Removing Duplicates Using set()
data = ["apple", "banana", "apple", "cherry"]
unique_fruits = list(set(data))
print(unique_fruits)
['banana', 'cherry', 'apple']
(Order may vary)
Common Mistakes
{}
is an empty dictionary, not a set — useset()
for an empty set- Sets do not support indexing or slicing
- Items in a set must be hashable (e.g., no lists)
Interview Tip
set()
is often used in coding problems involving uniqueness, duplicates, or fast lookups without ordering.
Summary
set()
converts iterables into a set of unique elements- Used to remove duplicates or perform set operations
- Unordered and unindexed collection
- Use
set()
, not{}
, to create an empty set
Practice Problem
Write a program to read 5 names from the user and display only the unique names entered.
names = []
for _ in range(5):
names.append(input("Enter a name: "))
print("Unique names:", set(names))