I have a list of words of variable length and want to add spaces (" ") to every word which is shorter than the longest word. I am later dividing this into sublists and therefore want every sublist in this list to be the same length!
My list of words could look like this:
words = ['sax', 'stol', 'kul', 'kök', 'äpple', 'rum', 'katt', 'toalettrulle']
So far my code looks like this:
len_list = []
for word in words:
len_list.append(len(word))
max_word_size = max(len_list)
for item in words:
len_item = len(item)
if len_item < max_word_size:
add_space = int(max_word_size - len_item)
words.append(" " * add_space)
else:
break
This gives me spaces added to the end of my list, which is not what I want. Does anyone have an idea how to fix this?