Skip to content

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.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
    print(i)

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c

In 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.

In python, we can access the set elements using the for loop. We can use the for loop to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
    print(i)

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c

In 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.

In python, we can access the set elements using the while loop. We can use the while loop to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
    print(data[i])
    i += 1

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c

In 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.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i, j in enumerate(data):
    print(i, j)

Output:

command
C:\Users\username>python tuple_index.py
0 a
1 e
2 b
3 d
4 g
5 f
6 c

In 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.

In python, we can access the set elements using the list() function. We can use the list() function to access the set elements.

tuple_index.py
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:

command
C:\Users\username>python tuple_index.py
a
e
b

In 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.

In python, we can access the set elements using the tuple() function. We can use the tuple() function to access the set elements.

tuple_index.py
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:

command
C:\Users\username>python tuple_index.py
a
e
b

In 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.

In python, we can access the set elements using the in keyword. We can use the in keyword to access the set elements.

tuple_index.py
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:

command
C:\Users\username>python tuple_index.py
a is in the set

In 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.

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.

tuple_index.py
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:

command
C:\Users\username>python tuple_index.py
m is not in the set

In 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.

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.


diagram a set answers one question quickly and refuses the other mermaid
There is no s[0], because a set has no positions to index -- the layout is decided by the hash of each value, not by insertion order. What you get in exchange is that asking whether a value is present costs roughly the same however large the set becomes.
sketch Why a set trades indexing for membership p5.js
A list keeps its elements in order, so finding one means walking until you hit it. A set stores each value in a slot chosen by its hash, so it can jump straight to where the value would be -- which is why there is nothing sensible for s[0] to mean. The measured cost of an absent-value lookup is on the right.
pch.quizTag pch.quizDefaultTitle
  1. What does `s[0]` do on a set?

    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.

  2. Membership on 10,000 elements measured 413 µs for a list and 0.192 µs for a set. Why?

    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.

  3. How do you get a specific element out of a set?

    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'.

  4. You have a list and will test membership many times. What is worth doing first?

    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.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading