This for loop works fine but not as a list comprehension. The comprehension creates empty elements for each iteration. Can someone explain what is wrong and how to fix it?
def GetAsciiList():
num_list = []
for num in range(32, 42, 1):
num_list.append((str(num), chr(num)+" "))
return num_list
def GetAsciiListLC():
num_list = []
num_list = [(num_list.append((str(num), chr(num)+" "))) for num in range(32, 42, 1)]
return num_list
print GetAsciiList()
print GetAsciiListLC()
console output:
[('32', ' '), ('33', '! '), ('34', '" '), ('35', '# '), ('36', '$ '), ('37', '% '), ('38', '& '), ('39', "' "), ('40', '( '), ('41', ') ')]
[None, None, None, None, None, None, None, None, None, None]