Access the Set
Access the Set
Section titled “Access the Set”In python, we can not access the set elements using the index number. Because the set is an unordered collection of items. So, we can not access the set elements using the index number. But we can access the set elements using the for loop.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
print(i)Output:
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
cIn this example, we declare a set and assign it to the variable data. We then print the set elements using the for loop. The output shows that the set elements are accessed using the for loop.
Access the Set using the for loop
Section titled “Access the Set using the for loop”In python, we can access the set elements using the for loop. We can use the for loop to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
print(i)Output:
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
cIn this example, we declare a set and assign it to the variable data. We then print the set elements using the for loop. The output shows that the set elements are accessed using the for loop.
Access the Set using the while loop
Section titled “Access the Set using the while loop”In python, we can access the set elements using the while loop. We can use the while loop to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
print(data[i])
i += 1Output:
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
cIn this example, we declare a set and assign it to the variable data. We then print the set elements using the while loop. The output shows that the set elements are accessed using the while loop.
Access the Set using the enumerate() function
Section titled “Access the Set using the enumerate() function”In python, we can access the set elements using the enumerate() function. We can use the enumerate() function to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i, j in enumerate(data):
print(i, j)Output:
C:\Users\username>python tuple_index.py
0 a
1 e
2 b
3 d
4 g
5 f
6 cIn this example, we declare a set and assign it to the variable data. We then print the set elements using the enumerate() function. The output shows that the set elements are accessed using the enumerate() function.
Access the Set using the list() function
Section titled “Access the Set using the list() function”In python, we can access the set elements using the list() function. We can use the list() function to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_list = list(data)
print(data_list[0])
print(data_list[1])
print(data_list[2])Output:
C:\Users\username>python tuple_index.py
a
e
bIn this example, we declare a set and assign it to the variable data. We then convert the set into a list using the list() function. We then print the list elements using the index number. The output shows that the list elements are accessed using the index number.
Access the Set using the tuple() function
Section titled “Access the Set using the tuple() function”In python, we can access the set elements using the tuple() function. We can use the tuple() function to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_tuple = tuple(data)
print(data_tuple[0])
print(data_tuple[1])
print(data_tuple[2])Output:
C:\Users\username>python tuple_index.py
a
e
bIn this example, we declare a set and assign it to the variable data. We then convert the set into a tuple using the tuple() function. We then print the tuple elements using the index number. The output shows that the tuple elements are accessed using the index number.
Access element using in keyword
Section titled “Access element using in keyword”In python, we can access the set elements using the in keyword. We can use the in keyword to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'a' in data:
print('a is in the set')
else:
print('a is not in the set')Output:
C:\Users\username>python tuple_index.py
a is in the setIn this example, we declare a set and assign it to the variable data. We then check if the element a is in the set or not using the in keyword. The output shows that the element a is in the set.
Access element using not in keyword
Section titled “Access element using not in keyword”In python, we can access the set elements using the not in keyword. We can use the not in keyword to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'm' not in data:
print('m is not in the set')
else:
print('m is in the set')Output:
C:\Users\username>python tuple_index.py
m is not in the setIn this example, we declare a set and assign it to the variable data. We then check if the element m is in the set or not using the not in keyword. The output shows that the element m is not in the set.
Conclusion
Section titled “Conclusion”In this tutorial, we learned how to access the set elements in python. We learn how to access the set elements using the for loop, while loop, enumerate() function, list() function, tuple() function, in keyword, and not in keyword. For more information, visit the official website of the python set. For more tutorials, visit our Python Central Hub.
flowchart TD A["a set"] --> B["s[0]"] B --> C["TypeError: 'set' object is not subscriptable"] A --> D["x in s"] D --> E["hash x, look in one bucket"] E --> F["roughly constant, whatever the size"] A --> G["for x in s:"] G --> H["works -- but the order is not something to rely on"] H --> I["need order or position? use a list"] F --> J["need fast membership or dedup? use a set"]
Check yourself
Section titled “Check yourself”-
What does `s[0]` do on a set?
A set has no positions. Elements live in slots chosen by their hash, so there is nothing for index 0 to mean. Iteration works, but the order is not something to depend on.
pch.quizShowAnswer
C — `TypeError: 'set' object is not subscriptable` — A set has no positions. Elements live in slots chosen by their hash, so there is nothing for index 0 to mean. Iteration works, but the order is not something to depend on.
-
Membership on 10,000 elements measured 413 µs for a list and 0.192 µs for a set. Why?
A miss is the worst case for a list — it must check everything before it can answer. The set's cost barely changed between 100 and 10,000 elements.
pch.quizShowAnswer
B — A list must compare each element; a set hashes the value and checks one place — A miss is the worst case for a list — it must check everything before it can answer. The set's cost barely changed between 100 and 10,000 elements.
-
How do you get a specific element out of a set?
If you need positions or a stable order, a set is the wrong container. Sets answer 'is this present' and 'what are the distinct values', not 'what is the third one'.
pch.quizShowAnswer
B — You cannot address one — convert to a list, or use `in` to test membership — If you need positions or a stable order, a set is the wrong container. Sets answer 'is this present' and 'what are the distinct values', not 'what is the third one'.
-
You have a list and will test membership many times. What is worth doing first?
The conversion is O(n) once and every later test is roughly constant. Sorting plus `bisect` also works and keeps order, but is more code for the same answer.
pch.quizShowAnswer
B — Convert it to a set once, then test against the set — The conversion is O(n) once and every later test is roughly constant. Sorting plus `bisect` also works and keeps order, but is more code for the same answer.
Try it: Access Set Elements Exercises
Section titled “Try it: Access Set Elements Exercises”Exercise 1 – Iterate Over a Set
Section titled “Exercise 1 – Iterate Over a Set”Exercise 2 – Convert to List
Section titled “Exercise 2 – Convert to List”Exercise 3 – Set Sum
Section titled “Exercise 3 – Set Sum”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading