+5 votes
in Programming Languages by (73.6k points)
edited by

I am trying to select some elements from a set, but it's giving an error: "TypeError: 'set' object is not subscriptable". The code is as follows:

t = [records[rc*j: rc*(j+1)] for j in range(5)]

In the above code, the variable "records" is a set. How to fix this error? 

1 Answer

+1 vote
by (355k points)
selected by
 
Best answer

You cannot use subscript with the set. That's why your code is giving an error. You can convert the set to a list and then can you subscript.

Make the following change and it should work.

records = list(records)

t = [records[rc*j: rc*(j+1)] for j in range(5)]


...