+4 votes
in Programming Languages by (17.7k 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

0 votes
by (35.8k points)

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)]

...