List comprehension (scientific computing with Python: boilerplate budget app)

I am just trying to write the create_spend_chart(categories) method for the budget app project.

First of all: I do not know if this is kind of a “spoiler” for solving this project.
If yes, please let me know and I will delete this topic again.
I would be glad if you could tell me then where I should ask this question.

Regardless if you think it is smart or not, I wanted to know the best way to solve my problem:
I want to create a list of the names of the passed category list and I want to fill every name with spaces so that in the end all names have the same length.

I already have a solution:

names = [ for c in categories]
max_namelength = max(len(n) for n in names)
names = [n.ljust(max_namelength, " ") for n in names]

Another solution is:
names = [[len( for c in categories]), " ") for c in categories]

But I kind of feel like that there is a smarter/shorter way to do this.
How would you solve this problem?

Thanks in advance!

Hi and welcome to the forum.

This is how I’d do it. The second involves a lot of recomputing the max length.

Ok. Thanks for the quick answer!