pythonlistcompilationesoteric-languages

Equalizing the lengths of all the lists within a list? (Python)


Important context: Dig on the esolang wiki

I am making an compiler for a esoteric programming language, using a 2d list to account for the language’s 2d nature. The problem comes when I need all the lists in the one mega list to be of same length.

This: [[“#”,”#”],[“#”,”#”,”#”]]

Needs be this: [[“#”,”#”,” “],[“#”,”#”,”#”]]

Thanks!


Solution

  • >>> mega_list = [["#","#"],["#","#","#"]]
    >>> for a in mega_list:
    ...     a.extend([" "] * (max(map(len, mega_list)) - len(a)))
    ...
    >>> mega_list
    [['#', '#', ' '], ['#', '#', '#']]