Iterate over a set in python

Iterate over a set in Python

Set is a collection of unique objects, which means there are no duplicate elements. And loop through the set to extract the objects each at a time, consider the following to know more.


for loop in set Python

The 'for' loop in Python is different form the 'for' loop in the other programming languages.


In Python, the 'for' loop does not require any update to be done. It automatically update after each iteration.


Python 'for' loop syntax

for variable in set_name:
    statements

Example

Input

a = {1, 2, 3, 4, 5}
for i in a:
    print(i)

Output

1
2
3
4
5


'for' loop over set with range() method

Loop over a set, like above can be done with the range() method.


We can not access the set using index because, they are unordered.


So we have to make use of iter() built-in method to convert the set into interable whose elements can be accessed by specifying index.

Program

Input

a = {1, 2, 3, 4, 5}
length = len(a)
for i in range(0, length):
    print(list(iter(a))[i])

Output

1
2
3
4
5

In looping over a set, range() method gives us the flexibility to slice the set.

Program

Input

b = {1, 2, 3, 4, 5, 6, 7, 8, 9}
for j in range(2, 5):
    print(list(iter(b))[j])

Output

3
4
5



Interval can be introduced while looping over a  set with the range() method.

Program

Input

b = {1, 2, 3, 4, 5, 6, 7, 8, 9}
length = len(b)
for step in range(0, length, 2):
    print(list(iter(b))[step])


Output

1
3
5
7
9


'for' loop over set with enumerate() method

The enumerate() method enumerates the set, with numbers.


Following is an example to enumerate the set.

Program

Input

b = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print(list(enumerate(b)))

Output

[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]


Here is an example, how you can fetch the content, got after enumerating the set. Both, set value and the enumerated value.

Program

Input

b = {1, 2, 3, 4, 5, 6, 7, 8, 9}
for i, j in list(enumerate(b)):
    print("Index: ", i, " Value: ", j
)


Output

Index:  0  Value:  1
Index:  1  Value:  2
Index:  2  Value:  3
Index:  3  Value:  4
Index:  4  Value:  5
Index:  5  Value:  6
Index:  6  Value:  7
Index:  7  Value:  8
Index:  8  Value:  9


Set comprehension

Set comprehension with 'for' loop gives a lot of information with 1 line of sentence and is more easy and readable.


Creating a set using set comprehension with 'for' loop

Following is the program which will create a set without using set comprehension.

Program

Input

a = set()
for value in range(0, 10):
a.append(value)

print(a)

Output

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}



Now this is how you can create a set using set comprehension with just one line of code. Which is so easy.

Program

Input

compre = {value for value in range(0, 10)}
print(compre)

Output

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}


Set comprehension with an expression

Let's put an expression at the beginning of for loop.


Here is an example, which will raise each value with the power of 2.

Program

Input

a = {1, 2, 3}
expo = {x**2 for x in a}
print(expo)

Output

{1, 4, 9}


Set/ tuple collection with set comprehension

Creation of set which will be the collection of set / tuple, using set comprehension.


The following code will have the collection of tuples, in which the two elements of each tuple will not be equal to each other.

Program

Input


col = {(x, y) for x in range(0, 5) for y in range(0, 5) if x!=y}
print(col)

Output

{(4, 0), (3, 4), (4, 3), (3, 1), (0, 2), (1, 0), (1, 3), (4, 2), (3, 0), (0, 1), (2, 4), (1, 2), (0, 4), (2, 1), (3, 2), (4, 1), (0, 3), (2, 0), (1, 4), (2, 3)}


Set comprehension with a function

Introducing a function at the beginning of the set comprehension, is also possible.


Set comprehension with built-in function

Here is an example, Which will exponentiate each value with 2.

Program

Input


a = {1, 2, 3}
pow2 = {pow(value, 2) for value in a}
print(pow2)

Output

{1, 4, 9}


Set comprehension using the user defined function

First of all we define a function, and then introduce it into the set comprehension.

Program

Input


def add2(x):
    return x + 2

a = {1, 2, 3, 4, 5}

add_with_2 = {add2(value) for value in a}

print(add_with_2)


Output


{3, 4, 5, 6, 7}




Conclusion

Sets are unordered and cannot be accessed using index, here we have learned how we can access a set elements using 'for' loop.

Looping through enumerated set, set comprehension, set comprehension with an expression and functions. 





Previous                          Next

If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)