. Advertisement .
..3..
. Advertisement .
..4..
“TypeError: ‘set’ object is not subscriptable in Python” is a common error that shows up in many ways. In this article we will give you some solutions to fix it. Read on it.
When Do You Get The Error “TypeError: ‘set’ object is not subscriptable in Python”?
When you try to slice or index a set, you easily get the following error message:
TypeError: 'set' object is not subscriptable in Python
You’re attempting to use indexing to access a set’s elements. Indexing, slicing, and any other sequence-like behavior are not supported by set objects. Therefore, the error “TypeError: ‘set’ object is not subscriptable in Python” happens.
How To Solve The Error “TypeError: ‘set’ object is not subscriptable in Python”?
Solution 1: Use the built-in list() function
To fix the problem “TypeError: ‘set’ object is not subscriptable in Python”, you can use the built-in list() function to change the list to a set. After that, let’s use indexing to access the last element of the list. Look at the following program:
# Specify set
numbers = {2, 4, 6, 7, 8}
# Change set to list
numbers_list = list(numbers)
# Changed list
print(numbers_list)
# Verifying type is list
print(type(numbers_list))
# The last element in the list
print(f'The last number in the list is: {numbers_list[-1]}')
Output
[2, 4, 6, 7, 8]
<class 'list'>
The last number in the list is: 8
Solution 2: Use colons to specify a dictionary
Another cause of this issue is that a set instead of a dictionary is wrongly created, and then you attempt to access it via indexing. In this case, you have to use colons to specify a dictionary to solve the problem “TypeError: ‘set’ object is not subscriptable in Python”. Look at the code below:
shop = {"T-shirt":25, "jeans":10, "dress":40, "shoes":20, "sandals":50}
inventory = []
hat = 100
def buy(item, hat):
if hat >= shop[item]:
print(f'You have bought: {item}')
inventory.append(item)
hat -= shop[item]
print(f'You have {hat} hat remaining')
else:
print(f'Sorry you do need {shop[item]} hat to buy: {item}')
item = input("What item would you like to buy for your quest?: ")
buy(item, hat)
Output
What item would you like to buy for your quest?: T-shirt
You have bought: T-shirt
You have 75 hats remaining
Solution 3: Use itertools.islice in a while loop
If you want to obtain 200 items at a time from the iterator which is built from the supplied set, you can use itertools.islice in a while loop as shown below:
from itertools import islice
def create(ids):
policy = {
'Statement': []
}
i = iter(ids)
while True:
chunk = list(islice(i, 200))
if not chunk:
break
policy['Statement'].append({
'Principal': {
'AWS': list(map(lambda id: f"arn:aws:iam::{id}:root", chunk))
}
})
return policy
Conclusion
You can always reread this post when you’re still stuck on “TypeError: ‘set’ object is not subscriptable in Python“. The options mentioned above are the quickest. In addition, if you still need help or have problems, we have a large population where everyone is usually eager to help. Finally, we wish all of you a wonderful day full of new code alternatives and appreciate your reading time.
Read more
Leave a comment