{"id":23998,"date":"2021-10-14T09:16:31","date_gmt":"2021-10-14T03:46:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=23998"},"modified":"2021-11-05T16:48:25","modified_gmt":"2021-11-05T11:18:25","slug":"python-program-for-set-len-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-set-len-method\/","title":{"rendered":"Python Program for Set len() Method"},"content":{"rendered":"

In the previous article, we have discussed Python Program for Set sorted() Method<\/a>
\nPython set:<\/strong><\/p>\n

Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.<\/p>\n

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given set = {'hello', 'this', 'is', 'btechgeeks'}<\/pre>\n

Output:<\/strong><\/p>\n

The given set is :\r\n{'is', 'btechgeeks', 'hello', 'this'}\r\nThe given set's length =  4<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given set = {34, 2, 56, 12, 1, 4, 2, 5}<\/pre>\n

Output:<\/strong><\/p>\n

The given set is :\r\n{1, 34, 2, 4, 5, 12, 56}\r\nThe given set's length = 7<\/pre>\n

Explanation:<\/strong><\/p>\n

Here the total items present in the set is 8, but set doesn't allow duplicate elements. \r\nHere 2 is the duplicate element. so, it gives the length of set as 7.<\/pre>\n

Program for Set len() Method in Python<\/h2>\n