I wanted to make an array of:

alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] a = alphabet[4, 10, 12, 5, 11, 6, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9] print(a) 

That is, the output will be written ['E', 'K', 'F' ... 'J']

TypeError: list indices must be integers or slices, not tuple

    2 answers 2

    Here the generator should be used:

     alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] a = [alphabet[i] for i in [4, 10, 12, 5, 11, 6, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9]] print(a) # ['E', 'K', 'M', 'F', 'L', 'G', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J'] 

      Or through lambda:

       alphabet = list(map(chr, range(65, 91))) a = list(map(lambda x: alphabet[x], [4, 10, 12, 5, 11, 6, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9])) print(a) 
      • The same x..n is in the other hand, only lambda and map added which need to be unpacked back into the list, instead of immediately generating the list. This is like scratching your left ear with your right heel, but you can do it - Andrey