Python Access Set Items | How to Access Set Elements with Examples

Python Access Set Items

In Python, sets are unordered collections of unique elements, which means you cannot access items by index like lists or tuples. Instead, to access set items, you typically use loops to iterate through all elements or check for membership using in keyword.

Python में sets unordered unique elements का collection होता है, इसलिए आप list या tuple की तरह index से items access नहीं कर सकते। set के items को access करने के लिए आप आमतौर पर loops से iterate करते हैं या in keyword से membership चेक करते हैं।

How to Access Set Items?

  • Use a for loop to iterate over all set elements.
  • Use the in operator to check if an element is in the set.
  • Convert the set to a list or tuple to access by index (if order matters temporarily).
  • Set के सभी elements पर for loop लगाएं।
  • in operator से membership चेक करें।
  • अगर index से access करना हो तो set को temporary list या tuple में बदलें।
Example 1: Iterate Through Set Using for Loop
myset = {10, 20, 30, 40}
for item in myset:
    print(item)

Print all items in the set using for loop.

for loop से set के सभी items प्रिंट करें।

Output:

10
20
30
40
Example 2: Check if Item Exists Using 'in'
myset = {'apple', 'banana', 'cherry'}
print('banana' in myset)
print('grape' in myset)

Check if 'banana' and 'grape' are in the set.

'banana' और 'grape' set में हैं या नहीं चेक करें।

Output:

True
False
Example 3: Convert Set to List to Access by Index
myset = {1, 2, 3, 4}
mylist = list(myset)
print(mylist[0])

Convert set to list to access first item by index 0 (order not guaranteed).

set को list में बदलकर index से first item access करें (क्रम निश्चित नहीं)।

Output:

1
Example 4: Use 'not in' to Check Non-membership
myset = {100, 200, 300}
print(400 not in myset)
print(200 not in myset)

Check if 400 is not in the set (True) and 200 is not in the set (False).

देखें कि 400 set में नहीं है (True) और 200 set में नहीं है (False)।

Output:

True
False
Example 5: Loop Through Set and Add 5 to Each Element
myset = {1, 2, 3}
for item in myset:
    print(item + 5)

Iterate through set and print each item plus 5.

set के हर item में 5 जोड़कर प्रिंट करें।

Output:

6
7
8
Example 6: Using set comprehension to filter items
myset = {10, 15, 20, 25, 30}
newset = {x for x in myset if x > 15}
print(newset)

Create a new set with elements greater than 15.

15 से बड़े elements से नया set बनाएं।

Output:

{20, 25, 30}
Example 7: Using next() with iter() to access one item
myset = {5, 10, 15}
item = next(iter(myset))
print(item)

Get one item from set using iterator (order not guaranteed).

iterator से set का एक item लें (क्रम निश्चित नहीं)।

Output:

5
Example 8: Check if Set is Empty
myset = set()
if not myset:
    print('Set is empty')

Check if set has no items.

देखें कि set खाली है या नहीं।

Output:

Set is empty
Example 9: Using for loop with break to find item
myset = {2, 4, 6, 8}
for item in myset:
    if item == 6:
        print('Found 6')
        break

Loop through set and stop when item 6 is found.

set में item 6 मिलने पर loop रोकें।

Output:

Found 6
Example 10: Accessing elements after sorting set (converted to list)
myset = {3, 1, 4, 2}
sorted_list = sorted(myset)
print(sorted_list[0])

Sort set, convert to list, and access first element.

set को sort करके list में बदलें और पहला element access करें।

Output:

1