Padding lists inside nested list to make all of uniform length
Question:
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:
mytable.append("")
else:
break
mytable2.append(mytable)
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]
else:
break
mytable3.append(mytable)
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', '']]
Answers:
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)))
print(tableData)
Output:
[['apples', 'oranges', 'cherries', 'banana', ''], ['Alice', 'Bob', 'Carol', 'David', 'Katherine'], ['dogs', 'cats', 'moose', 'goose', '']]
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)
inside_list.append("")
Running this will create your final list as
tableData = [['apples', 'oranges', 'cherries', 'banana', ''],
['Alice', 'Bob', 'Carol', 'David', 'Katherine'],
['dogs', 'cats', 'moose', 'goose', '']]
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:
mytable.append("")
else:
break
mytable2.append(mytable)
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]
else:
break
mytable3.append(mytable)
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', '']]
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)))
print(tableData)
Output:
[['apples', 'oranges', 'cherries', 'banana', ''], ['Alice', 'Bob', 'Carol', 'David', 'Katherine'], ['dogs', 'cats', 'moose', 'goose', '']]
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)
inside_list.append("")
Running this will create your final list as
tableData = [['apples', 'oranges', 'cherries', 'banana', ''],
['Alice', 'Bob', 'Carol', 'David', 'Katherine'],
['dogs', 'cats', 'moose', 'goose', '']]