How to Get Elements From Set in Python Examples

Last Updated on June 10, 2021 by Roshan Parihar

In this tutorial, learn how to get elements from Set in Python. The short answer is to use the for loop of Python to access each element of Set and print them in the output.

You can also find the smallest and largest element in a Set with min and max in Python. Let’s find out the methods with the example given below.

How to Get Elements From Set Using For Loop in Python

To get each item of a Set, you can use the for loop of Python that accesses each item on each iteration. If you want to get the matching element in a Set, you can use the if condition to match with the element and print the result.

Output

10
13.2
Bilal
Feroz
Shyam
Ram

The above example prints each element one by one with one element at one line. The elements get printed on each iteration over Set elements in Python.

Check If Specified Element is Present in a Set Using Python

If you want to check the given element is present in the Set using Python. You can use the simple method that checks the element in a Set. It gives the result in boolean format (True/False). If the element is present in the Set, it gives True otherwise False.

Output

True

The output in the above example shows that the specified element is present in the Set. The example checks a single element at a time in the Set variable.

Get Smallest Value Elements From Numeric Set Using Python

You can find the smallest value from the Set variable using the min function. It takes a single argument as the Set variable to get the lowest element from it. The Set variable contains all the elements in numeric format.

Output

3

The above example contains an output that shows that 3 is the smallest item in the given Set variable.

Note: To find the smallest and largest value in a Set, all the elements should be numeric to make it work.

Find Largest Value Item in a Numeric Set Using Python

The largest value can be retrieved from a Set using the max() function. It takes a set variable as its argument to retrieve the largest numeric value from it.

Output

21

The above example shows that 21 is the largest value among all the elements of a Set.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.