Did you know in python you can stick a for loop in the list brackets and just BAM have a list? No “append” check it:

l=[chr(i+65) for i in range(0,26)]
print(l)

> [‘A’, ‘B’, ‘C’, … , ‘Z’]

(it will be the letters A-Z since the unicode for the capital letter starts at 65, chr renders these numbers as letters.)

Is that classy? For some reason this neat little feature isn’t really taught. I guess with a loop and appending you have not flexibility— but still this is cool. #python #coding #tricks

Follow

@futurebird In our first-year seminar section (a gentle introduction to data science), we taught list comprehensions instead of loops. It turned out the only need we had for loops was to transform or filter lists, so there was no need for the more general for loop.

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.