jessibird jessibird - 1 month ago 12
Python Question

What is the function doing in the python list?

char_set = [False for _ in range(128)]
for char in string:
val = ord(char)
if char_set[val]:
# Char already found in string
return False
char_set[val] = True

I'm trying to decipher this code but don't understand what char_set is doing? The part inside the list is confusing.


[False for _ in range(128)] is a list comprehension that will return a list of 128 Falses. In other words, char_set will be [False, False, ...] with 128 False. The _ represents a throwaway value (a value which is created by the range() function, but that the program does not need to do anything with. In this case, _ is the number representing the iterations of the range() function (i.e., 0, 1, 2, etc.).