Languages
[Edit]
EN

Python - access set items

0 points
Created by:
Joanne-Senior
530

In this article, we would like to show you how to access set items in Python.

You can't directly access set items by referring to an index or a key. What you can do is to iterate through the set to process its items or check if the set contains a specific value.

1. Iterate through set

In below example, we use simple for loop to iterate through set and print the values.

my_set = {'A', 'B', 'C'}

for x in my_set:
    print(x)

Output:

A
B
C

2. Check if set contains specific value

In below, example we use in keyword to check if our set contains B value.

my_set = {'A', 'B', 'C'}

print('B' in my_set)  # True

Output:

True

Python - set (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join