Set comprehension

1


s = set()
for i in range(10):
    s.add(i)
print(s)

Execution result of 1


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

If this is included

Comprehension notation 1


s = {i for i in range(10)}
print(s)

Execution result of inclusion notation 1


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

As with list comprehensions To divide by 3 and put only the number with a remainder of 0 in the set

Comprehension notation 2


s = {i for i in range(10) if i % 3 == 0}
print(s)

Execution result of inclusion notation 2


{0, 3, 6, 9}

Recommended Posts

Set comprehension
Python comprehension
List comprehension
Comprehension notation
Python comprehension
List comprehension
Comprehension notation
About python comprehension
filter vs comprehension
Data set generation
Python set arithmetic
[Introduction to Udemy Python3 + Application] 62. Set comprehension notation
Python> Comprehension / Comprehension> List comprehension
Note: List comprehension