B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Access Set Items with Examples | LiveCodeProgramming

Python Access Set Items

Sets in Python are unordered, so they do not support indexing. But we can access set items using loops or by converting to a list.

Python में sets unordered होते हैं, इसलिए आप index से सीधे access नहीं कर सकते। लेकिन आप loop या list में बदलकर items access कर सकते हैं।

1. Loop Through a Set
my_set = {"Python", "Java", "C++"}
for item in my_set:
    print(item)
2. Check if Item Exists in Set
if "Java" in my_set:
    print("Java is in the set")
3. Using enumerate()
for index, value in enumerate(my_set):
    print(index, value)
4. Convert Set to List to Access by Index
my_list = list(my_set)
print(my_list[0])