Example 1: Python Set clear() Method Example

Python3




test_set = {1, 2, 3, 4}
test_set.clear()
print("After clear() on test_set:", test_set)


Output:

After clear() on test_set: set()

The time complexity of the clear() method on a set in Python is O(n), where n is the number of elements in the set.

The auxiliary space complexity of the clear() method is also O(1), as it only needs to delete the existing elements in the set and not allocate any new memory.

Python Set clear() Method

Python Set clear() method removes all elements from the set.

Similar Reads

Python Set clear() Method Syntax:

Syntax: set.clear() parameters: The clear() method doesn’t take any parameters.  Return: None...

Example 1: Python Set clear() Method Example

Python3 test_set = {1, 2, 3, 4} test_set.clear() print("After clear() on test_set:", test_set)...

Example 2: Python Set clear() Method on a Set of Strings

...

Contact Us