Open In App

Find the size of a Set in Python

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set class represents the mathematical notion of a set. The size of a set means the amount of memory (in bytes) occupied by a set object. In this article, we will learn various ways to get the size of a python set.

1.Using getsizeof() function:

The getsizeof() function belongs to the python’s sys module. It has been implemented in the below example. Example 1: 

Python3




import sys
 
# sample Sets
Set1 = {"A", 1, "B", 2, "C", 3}
Set2 = {"Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu"}
Set3 = {(1, "Lion"), ( 2, "Tiger"), (3, "Fox")}
 
# print the sizes of sample Sets
print("Size of Set1: " + str(sys.getsizeof(Set1)) + "bytes")
print("Size of Set2: " + str(sys.getsizeof(Set2)) + "bytes")
print("Size of Set3: " + str(sys.getsizeof(Set3)) + "bytes")


Output:

Size of Set1: 736bytes
Size of Set2: 736bytes
Size of Set3: 224bytes

Time Complexity: O(n)
Auxiliary Space: O(1)

Note:The sys.getsizeof() function includes the marginal space usage, which includes the garbage collection overhead for the object. Meaning it returns the total space occupied by the object in addition to the garbage collection overhead for the spaces being used.

1.Using inbuilt __sizeof__() method:

Python also has an inbuilt __sizeof__() method to determine the space allocation of an object without any additional garbage value. It has been implemented in the below example. Example 2: 

Python3




import sys
 
# sample Sets
Set1 = {"A", 1, "B", 2, "C", 3}
Set2 = {"Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu"}
Set3 = {(1, "Lion"), ( 2, "Tiger"), (3, "Fox")}
 
# print the sizes of sample Sets
print("Size of Set1: " + str(Set1.__sizeof__()) + "bytes")
print("Size of Set2: " + str(Set2.__sizeof__()) + "bytes")
print("Size of Set3: " + str(Set3.__sizeof__()) + "bytes")


Output:

Size of Set1: 712bytes
Size of Set2: 712bytes
Size of Set3: 200bytes


Similar Reads

Change the label size and tick label size of colorbar using Matplotlib in Python
In this article, we will learn how to change the label size and tick label size of colorbar in Matplotlib using Python. Labels are a kind of assigning name that can be applied to any node in the graph. They are a name only and so labels are either present or absent. To properly label a graph, helps to identify the x-axis and y-axis. Each tick mark
2 min read
PyQt5 - How to adjust size of ComboBox according to the items size
In this article we will see how we can adjust the combo box according to the size of item which has maximum size among the item list. By default we use setGeometry method and setSize method to adjust the size but it will not automatically adjust the size according to the item size. In order to adjust the size according to the maximum length item si
2 min read
Python program to get all subsets of given size of a set
Given a set, write a Python program to generate all possible subset of size n of given set within a list. Examples: Input : {1, 2, 3}, n = 2 Output : [{1, 2}, {1, 3}, {2, 3}] Input : {1, 2, 3, 4}, n = 3 Output : [{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}] We have already discussed the same problem using the Naive approach in this article. This art
3 min read
Python Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).  Example:  Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL  Recommended: Please solve it on "PRACTICE" first, before moving on to the so
3 min read
Python Program For Reversing A Linked List In Groups Of Given Size- Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->NULL and k = 5 Output: 5->4->3->2->1->8->7->6->NULL. Recommended: Please solve it on "PRACTICE" first, before moving on to
3 min read
Python Counter to find the size of largest subset of anagram words
Given an array of n string containing lowercase letters. Find the size of largest subset of string which are anagram of each others. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other. Examples: Input: ant magenta magnate tan
5 min read
Find size of a list in Python
A list is a collection data type that is ordered and changeable. A list can have duplicate entries as well. Here, the task is to find the number of entries in a list in Python. Examples: Input: a = [1, 2, 3, 1, 2, 3]Output: 6Explanation: The output is 6 because the number of entries in the list a is also 6.Find the Length of a List in PythonBelow a
3 min read
How to find size of an object in Python?
In python, the usage of sys.getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes. It takes at most two arguments i.e Object itself. Note: Only the memory consumption directly attributed to the object is accounted for, not the memory cons
2 min read
Find the size of a list - Python
In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using getsizeof() function: The getsizeof() function belong
2 min read
Find the size of a Tuple in Python
Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. The size of a Tuple me
2 min read
Practice Tags :
three90RightbarBannerImg