i'm trying to write some program that take lines that represent matrix from user input and store it in list of lists. If the lists are not the same length, we add zeros to all the lists that are not in the same length of the longest one. after that we store all the lists in dictionary.
I just want to know if I can do it in a more efficient way
I would like to improve the second loop and instead of a nested loop do it in one loop, is that possible?
For example: the input is:
1,2,4,6
3,4
2,4,1
end
the output should be :
{1:[1,2,4,6], 2:[3,4,0,0],3:[2,4,1,0]}
my code is:
lst = []
sub_list = []
while sub_list != 'end':
sub_list = input()
if sub_list !='end':
lst.append(list(sub_list.split(',')))
lst = [list(map(int, sublist)) for sublist in lst]
l = len(max(lst,key=len))
for i in lst:
while len(i) != l:
i.append(0)
res = {idx + 1: lst[idx] for idx in range(len(lst))}
print(str(res))