Python Forum
TypeError: list indices must be integers or slices, not range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: list indices must be integers or slices, not range (/thread-37004.html)



TypeError: list indices must be integers or slices, not range - Anldra12 - Apr-22-2022

Get the TypeError: list indices must be integers or slices, not range

def getDistinctX(x_subi, F):
    x_subi = []
    ind = random.randint(1, 5)
    ind = range(0, (len(F)-1))
    x = F[ind]
    if not x in x_subi:
        return x
    else:
        x = getDistinctX(x_subi, F)
        return x
Error:
Traceback (most recent call last): File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 271, in <module> runPackage([], []) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 255, in runPackage predefinedVars, returnK = initiateScheme(predefinedVars) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 56, in initiateScheme recovered_k = runScheme(t, n, secret_k, q, Field) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 101, in runScheme x = getDistinctX(x_subi, Field) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 181, in getDistinctX x = F[ind] TypeError: list indices must be integers or slices, not range Process finished with exit code 1



RE: TypeError: list indices must be integers or slices, not range - Gribouillis - Apr-22-2022

It looks clear, if F is a list and ind is a range object, F[ind] is not a valid operation. It would be valid if ind was an integer or a slice object. Perhaps you meant F[:-1] instead of F[ind]?


RE: TypeError: list indices must be integers or slices, not range - Anldra12 - Apr-22-2022

@Gribouillis Thanks you yeah I find the mistake to change it F(:-1):