Sitemap

Member-only story

Set in Python [3]

3 min readOct 29, 2024

--

Hello,

Press enter or click to view image in full size
Photo by Joseph Barrientos on Unsplash

81. Set of Numbers Divisible by 5 and 7 in a Range

python
Copy code
divisible_by_5_and_7 = {x for x in range(1, 101) if x % 5 == 0 and x % 7 == 0}  # {35, 70}

82. Check if All Elements in Set are Unique (always true for a set)

python
Copy code
is_unique = len(my_set) == len(set(my_set))  # Always True for a set since elements are unique by nature

83. Flatten a Set of Sets of Integers

python
Copy code
nested_set = {{1, 2}, {3, 4}, {5}}
flat_set = {element for subset in nested_set for element in subset} # {1, 2, 3, 4, 5}

84. Filter Set to Only Uppercase Strings

python
Copy code
strings = {"Hello", "WORLD", "Python", "CODE"}
uppercase_strings = {s for s in strings if s.isupper()} # {'WORLD', 'CODE'}

85. Find Subset of Set with Length Equal to 2

python
Copy code

--

--