Padding lists inside nested list to make all of uniform length


I am trying to solve a problem that given a nested list containing lists of variable size, create a nested list that contains lists of uniform length equal to the maximum length among the lists inside the given nested list. Append empty strings as padding.

Input list:

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David','Katherine'],
             ['dogs', 'cats', 'moose', 'goose']]

This is the list i made where width is the max value out of all sub-lists:

    mytable = []
    mytable2 = []
    mytable3 = []
    for i in range(len(tableData)):
        for j in range(width):
            if len(mytable) < width:

I am trying to replace the values:(I can’t think of conditions to stop the loop from overwriting the values.)

    for i in range(len(tableData)):
        for j in range(len(tableData[i])):
            if mytable[j] == "":
                mytable[j] = tableData[i][j]

The output I am getting:(all three sub-lists are getting overwritten simultaneously)

[['apples', 'oranges', 'cherries', 'banana', ''],
 ['apples', 'oranges', 'cherries', 'banana', ''], 
['apples', 'oranges', 'cherries', 'banana', '']]
Asked By: Surya Kannan



Find the maximum length of a sub-list. Iterate over the sub-lists and extend them accordingly:

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David','Katherine'],
             ['dogs', 'cats', 'moose', 'goose']]

_max = max(map(len, tableData))

for e in tableData:
    e.extend([''] * (_max - len(e)))



[['apples', 'oranges', 'cherries', 'banana', ''], ['Alice', 'Bob', 'Carol', 'David', 'Katherine'], ['dogs', 'cats', 'moose', 'goose', '']]
Answered By: Pingu

I will show you the better way to do it. Read through the whole answer to understand the code and procedure. I will not be using any more functions than those being used in the code snippets you have provided.
Assuming the Input nested list is

tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David','Katherine'],
             ['dogs', 'cats', 'moose', 'goose']]

Now, let’s iterate over this nested list to find the maximum length of lists inside this nested list.

max_length = 0
for inside_list in tableData:
    if len(inside_list) > max_length:
        max_length = len(inside_list)

Outputs 5

Now, just iterate over the lists inside tableData and find how shorter are they than the maximum. Then append the empty string that many times to pad them to a uniform length.

for inside_list in tableData:
    shorter = max_length - len(inside_list)
    for i in range(shorter)

Running this will create your final list as

tableData = [['apples', 'oranges', 'cherries', 'banana', ''],
             ['Alice', 'Bob', 'Carol', 'David', 'Katherine'],
             ['dogs', 'cats', 'moose', 'goose', '']]
Answered By: Mathpdegeek497
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.