Pyhton: Retrieve the length of a list value that's within a list value












1















I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.



I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.



In the question, one is given a grid as follows:



grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]


Using loops only, one is then required to produce the following output:



..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....


My solution (which works) to this problem is as follows:



for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')

print()


My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.



My question, therefore, is how can I retrieve the len of grid[0] in general variable terms?



In other words, how can I retrieve the length of a list within a list?










share|improve this question























  • So, do you want a nice solution with zip or are we stuck to only for and print here? :)

    – timgeb
    Nov 14 '18 at 8:32











  • Kind of confusing what exactly you are asking, but just do len(grid(row_number))

    – Tomothy32
    Nov 14 '18 at 8:33











  • @Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.

    – Mexen
    Nov 14 '18 at 8:49






  • 1





    @timgeb Thanks. The book is yet to introduce zip so just for/while and print.

    – Mexen
    Nov 14 '18 at 8:51











  • Do you want something like for sublist in grid: len(sublist)?

    – Tomothy32
    Nov 14 '18 at 9:00
















1















I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.



I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.



In the question, one is given a grid as follows:



grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]


Using loops only, one is then required to produce the following output:



..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....


My solution (which works) to this problem is as follows:



for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')

print()


My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.



My question, therefore, is how can I retrieve the len of grid[0] in general variable terms?



In other words, how can I retrieve the length of a list within a list?










share|improve this question























  • So, do you want a nice solution with zip or are we stuck to only for and print here? :)

    – timgeb
    Nov 14 '18 at 8:32











  • Kind of confusing what exactly you are asking, but just do len(grid(row_number))

    – Tomothy32
    Nov 14 '18 at 8:33











  • @Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.

    – Mexen
    Nov 14 '18 at 8:49






  • 1





    @timgeb Thanks. The book is yet to introduce zip so just for/while and print.

    – Mexen
    Nov 14 '18 at 8:51











  • Do you want something like for sublist in grid: len(sublist)?

    – Tomothy32
    Nov 14 '18 at 9:00














1












1








1


1






I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.



I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.



In the question, one is given a grid as follows:



grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]


Using loops only, one is then required to produce the following output:



..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....


My solution (which works) to this problem is as follows:



for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')

print()


My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.



My question, therefore, is how can I retrieve the len of grid[0] in general variable terms?



In other words, how can I retrieve the length of a list within a list?










share|improve this question














I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.



I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.



In the question, one is given a grid as follows:



grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]


Using loops only, one is then required to produce the following output:



..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....


My solution (which works) to this problem is as follows:



for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')

print()


My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.



My question, therefore, is how can I retrieve the len of grid[0] in general variable terms?



In other words, how can I retrieve the length of a list within a list?







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 8:28









MexenMexen

18319




18319













  • So, do you want a nice solution with zip or are we stuck to only for and print here? :)

    – timgeb
    Nov 14 '18 at 8:32











  • Kind of confusing what exactly you are asking, but just do len(grid(row_number))

    – Tomothy32
    Nov 14 '18 at 8:33











  • @Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.

    – Mexen
    Nov 14 '18 at 8:49






  • 1





    @timgeb Thanks. The book is yet to introduce zip so just for/while and print.

    – Mexen
    Nov 14 '18 at 8:51











  • Do you want something like for sublist in grid: len(sublist)?

    – Tomothy32
    Nov 14 '18 at 9:00



















  • So, do you want a nice solution with zip or are we stuck to only for and print here? :)

    – timgeb
    Nov 14 '18 at 8:32











  • Kind of confusing what exactly you are asking, but just do len(grid(row_number))

    – Tomothy32
    Nov 14 '18 at 8:33











  • @Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.

    – Mexen
    Nov 14 '18 at 8:49






  • 1





    @timgeb Thanks. The book is yet to introduce zip so just for/while and print.

    – Mexen
    Nov 14 '18 at 8:51











  • Do you want something like for sublist in grid: len(sublist)?

    – Tomothy32
    Nov 14 '18 at 9:00

















So, do you want a nice solution with zip or are we stuck to only for and print here? :)

– timgeb
Nov 14 '18 at 8:32





So, do you want a nice solution with zip or are we stuck to only for and print here? :)

– timgeb
Nov 14 '18 at 8:32













Kind of confusing what exactly you are asking, but just do len(grid(row_number))

– Tomothy32
Nov 14 '18 at 8:33





Kind of confusing what exactly you are asking, but just do len(grid(row_number))

– Tomothy32
Nov 14 '18 at 8:33













@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.

– Mexen
Nov 14 '18 at 8:49





@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.

– Mexen
Nov 14 '18 at 8:49




1




1





@timgeb Thanks. The book is yet to introduce zip so just for/while and print.

– Mexen
Nov 14 '18 at 8:51





@timgeb Thanks. The book is yet to introduce zip so just for/while and print.

– Mexen
Nov 14 '18 at 8:51













Do you want something like for sublist in grid: len(sublist)?

– Tomothy32
Nov 14 '18 at 9:00





Do you want something like for sublist in grid: len(sublist)?

– Tomothy32
Nov 14 '18 at 9:00












5 Answers
5






active

oldest

votes


















2














You don't actually need to retrieve length.



You can do it in one line without knowing dimensions:



print('n'.join(map(lambda x: ''.join(x), zip(*grid))))


Step by step:





  1. zip(*grid) will transpose your 2-d array



>>> list(zip(*grid))
[('.', '.', '0', '0', '.', '0', '0', '.', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '.', '0', '0', '0', '0', '0', '.', '.'),
('.', '.', '.', '0', '0', '0', '.', '.', '.'),
('.', '.', '.', '.', '0', '.', '.', '.', '.')]




  1. map(lambda x: ''.join(x), zip(*grid)) will join every line



>>> list(map(lambda x: ''.join(x), zip(*grid)))
['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']



  1. And finally 'n'.join joins with separator as new line



>>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....


Note: we could also omit 3rd step by printing with sep='n, however in Python3:
Why does map return a map object instead of a list in Python 3? and we would need to convert it to list before printing it. Like so:




print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')





share|improve this answer





















  • 1





    Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

    – Mexen
    Nov 14 '18 at 13:51



















2














For example taking the longes of all the content list:



>>> grid = [['.', '.', '.', '.', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['0', '0', '0', '0', '0', '.'],
... ['.', '0', '0', '0', '0', '0'],
... ['0', '0', '0', '0', '0', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['.', '.', '.', '.', '.', '.']]
>>>
>>> max(map(len, grid))
6


BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid)):



>>> for l in zip(*grid):
... print("".join(l))
...
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....





share|improve this answer





















  • 1





    You don't need to convert your zip object into list since you iterating over it.

    – vishes_shell
    Nov 14 '18 at 8:47



















1














If you look at your desired output, you'll observe that final matrix is transpose of your given list.



The transpose of a matrix could be found by using numpy transpose function.



You could use a list comprehension in combination with join method in order to achieve what you want.



import numpy as np
transpose = np.array(grid).transpose()
print('n'.join([''.join(row) for row in transpose]))


Output



..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....





share|improve this answer


























  • I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

    – Mexen
    Nov 14 '18 at 13:52











  • @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

    – Mihai Alexandru-Ionut
    Nov 14 '18 at 14:49



















1














If you really must get the length of a list in a list, just use len(grid[index]):



for i in range(len(grid)):
for j in range(len(grid[i])):
print(mylist[i][j], end='')
print()


However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use



for row in mylist:
for element in row:
print(element, end='')
print()





share|improve this answer































    1














    Another way of doing this is to start by transposing the grid, then just loop over the elements of each row



     grid = [['.', '.', '.', '.', '.', '.'],
    ['.', '0', '0', '.', '.', '.'],
    ['0', '0', '0', '0', '.', '.'],
    ['0', '0', '0', '0', '0', '.'],
    ['.', '0', '0', '0', '0', '0'],
    ['0', '0', '0', '0', '0', '.'],
    ['0', '0', '0', '0', '.', '.'],
    ['.', '0', '0', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.']]
    grid2=zip(*grid)
    # print(grid2)
    for row in grid2:
    for el in row:
    print(el, end='')
    print('n')





    share|improve this answer





















    • 1





      You don't actually need to convert grid2 into a list, since you can iterate over it.

      – vishes_shell
      Nov 14 '18 at 8:45











    • Very true, editing

      – robotHamster
      Nov 14 '18 at 8:47











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295848%2fpyhton-retrieve-the-length-of-a-list-value-thats-within-a-list-value%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You don't actually need to retrieve length.



    You can do it in one line without knowing dimensions:



    print('n'.join(map(lambda x: ''.join(x), zip(*grid))))


    Step by step:





    1. zip(*grid) will transpose your 2-d array



    >>> list(zip(*grid))
    [('.', '.', '0', '0', '.', '0', '0', '.', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '.', '0', '0', '0', '0', '0', '.', '.'),
    ('.', '.', '.', '0', '0', '0', '.', '.', '.'),
    ('.', '.', '.', '.', '0', '.', '.', '.', '.')]




    1. map(lambda x: ''.join(x), zip(*grid)) will join every line



    >>> list(map(lambda x: ''.join(x), zip(*grid)))
    ['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']



    1. And finally 'n'.join joins with separator as new line



    >>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....


    Note: we could also omit 3rd step by printing with sep='n, however in Python3:
    Why does map return a map object instead of a list in Python 3? and we would need to convert it to list before printing it. Like so:




    print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')





    share|improve this answer





















    • 1





      Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

      – Mexen
      Nov 14 '18 at 13:51
















    2














    You don't actually need to retrieve length.



    You can do it in one line without knowing dimensions:



    print('n'.join(map(lambda x: ''.join(x), zip(*grid))))


    Step by step:





    1. zip(*grid) will transpose your 2-d array



    >>> list(zip(*grid))
    [('.', '.', '0', '0', '.', '0', '0', '.', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '.', '0', '0', '0', '0', '0', '.', '.'),
    ('.', '.', '.', '0', '0', '0', '.', '.', '.'),
    ('.', '.', '.', '.', '0', '.', '.', '.', '.')]




    1. map(lambda x: ''.join(x), zip(*grid)) will join every line



    >>> list(map(lambda x: ''.join(x), zip(*grid)))
    ['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']



    1. And finally 'n'.join joins with separator as new line



    >>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....


    Note: we could also omit 3rd step by printing with sep='n, however in Python3:
    Why does map return a map object instead of a list in Python 3? and we would need to convert it to list before printing it. Like so:




    print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')





    share|improve this answer





















    • 1





      Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

      – Mexen
      Nov 14 '18 at 13:51














    2












    2








    2







    You don't actually need to retrieve length.



    You can do it in one line without knowing dimensions:



    print('n'.join(map(lambda x: ''.join(x), zip(*grid))))


    Step by step:





    1. zip(*grid) will transpose your 2-d array



    >>> list(zip(*grid))
    [('.', '.', '0', '0', '.', '0', '0', '.', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '.', '0', '0', '0', '0', '0', '.', '.'),
    ('.', '.', '.', '0', '0', '0', '.', '.', '.'),
    ('.', '.', '.', '.', '0', '.', '.', '.', '.')]




    1. map(lambda x: ''.join(x), zip(*grid)) will join every line



    >>> list(map(lambda x: ''.join(x), zip(*grid)))
    ['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']



    1. And finally 'n'.join joins with separator as new line



    >>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....


    Note: we could also omit 3rd step by printing with sep='n, however in Python3:
    Why does map return a map object instead of a list in Python 3? and we would need to convert it to list before printing it. Like so:




    print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')





    share|improve this answer















    You don't actually need to retrieve length.



    You can do it in one line without knowing dimensions:



    print('n'.join(map(lambda x: ''.join(x), zip(*grid))))


    Step by step:





    1. zip(*grid) will transpose your 2-d array



    >>> list(zip(*grid))
    [('.', '.', '0', '0', '.', '0', '0', '.', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '0', '0', '0', '0', '0', '0', '0', '.'),
    ('.', '.', '0', '0', '0', '0', '0', '.', '.'),
    ('.', '.', '.', '0', '0', '0', '.', '.', '.'),
    ('.', '.', '.', '.', '0', '.', '.', '.', '.')]




    1. map(lambda x: ''.join(x), zip(*grid)) will join every line



    >>> list(map(lambda x: ''.join(x), zip(*grid)))
    ['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']



    1. And finally 'n'.join joins with separator as new line



    >>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....


    Note: we could also omit 3rd step by printing with sep='n, however in Python3:
    Why does map return a map object instead of a list in Python 3? and we would need to convert it to list before printing it. Like so:




    print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 8:42

























    answered Nov 14 '18 at 8:34









    vishes_shellvishes_shell

    10.4k34045




    10.4k34045








    • 1





      Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

      – Mexen
      Nov 14 '18 at 13:51














    • 1





      Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

      – Mexen
      Nov 14 '18 at 13:51








    1




    1





    Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

    – Mexen
    Nov 14 '18 at 13:51





    Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!

    – Mexen
    Nov 14 '18 at 13:51













    2














    For example taking the longes of all the content list:



    >>> grid = [['.', '.', '.', '.', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['.', '0', '0', '0', '0', '0'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['.', '.', '.', '.', '.', '.']]
    >>>
    >>> max(map(len, grid))
    6


    BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid)):



    >>> for l in zip(*grid):
    ... print("".join(l))
    ...
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....





    share|improve this answer





















    • 1





      You don't need to convert your zip object into list since you iterating over it.

      – vishes_shell
      Nov 14 '18 at 8:47
















    2














    For example taking the longes of all the content list:



    >>> grid = [['.', '.', '.', '.', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['.', '0', '0', '0', '0', '0'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['.', '.', '.', '.', '.', '.']]
    >>>
    >>> max(map(len, grid))
    6


    BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid)):



    >>> for l in zip(*grid):
    ... print("".join(l))
    ...
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....





    share|improve this answer





















    • 1





      You don't need to convert your zip object into list since you iterating over it.

      – vishes_shell
      Nov 14 '18 at 8:47














    2












    2








    2







    For example taking the longes of all the content list:



    >>> grid = [['.', '.', '.', '.', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['.', '0', '0', '0', '0', '0'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['.', '.', '.', '.', '.', '.']]
    >>>
    >>> max(map(len, grid))
    6


    BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid)):



    >>> for l in zip(*grid):
    ... print("".join(l))
    ...
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....





    share|improve this answer















    For example taking the longes of all the content list:



    >>> grid = [['.', '.', '.', '.', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['.', '0', '0', '0', '0', '0'],
    ... ['0', '0', '0', '0', '0', '.'],
    ... ['0', '0', '0', '0', '.', '.'],
    ... ['.', '0', '0', '.', '.', '.'],
    ... ['.', '.', '.', '.', '.', '.']]
    >>>
    >>> max(map(len, grid))
    6


    BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid)):



    >>> for l in zip(*grid):
    ... print("".join(l))
    ...
    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 8:47

























    answered Nov 14 '18 at 8:32









    NetwaveNetwave

    12.5k22144




    12.5k22144








    • 1





      You don't need to convert your zip object into list since you iterating over it.

      – vishes_shell
      Nov 14 '18 at 8:47














    • 1





      You don't need to convert your zip object into list since you iterating over it.

      – vishes_shell
      Nov 14 '18 at 8:47








    1




    1





    You don't need to convert your zip object into list since you iterating over it.

    – vishes_shell
    Nov 14 '18 at 8:47





    You don't need to convert your zip object into list since you iterating over it.

    – vishes_shell
    Nov 14 '18 at 8:47











    1














    If you look at your desired output, you'll observe that final matrix is transpose of your given list.



    The transpose of a matrix could be found by using numpy transpose function.



    You could use a list comprehension in combination with join method in order to achieve what you want.



    import numpy as np
    transpose = np.array(grid).transpose()
    print('n'.join([''.join(row) for row in transpose]))


    Output



    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....





    share|improve this answer


























    • I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

      – Mexen
      Nov 14 '18 at 13:52











    • @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

      – Mihai Alexandru-Ionut
      Nov 14 '18 at 14:49
















    1














    If you look at your desired output, you'll observe that final matrix is transpose of your given list.



    The transpose of a matrix could be found by using numpy transpose function.



    You could use a list comprehension in combination with join method in order to achieve what you want.



    import numpy as np
    transpose = np.array(grid).transpose()
    print('n'.join([''.join(row) for row in transpose]))


    Output



    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....





    share|improve this answer


























    • I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

      – Mexen
      Nov 14 '18 at 13:52











    • @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

      – Mihai Alexandru-Ionut
      Nov 14 '18 at 14:49














    1












    1








    1







    If you look at your desired output, you'll observe that final matrix is transpose of your given list.



    The transpose of a matrix could be found by using numpy transpose function.



    You could use a list comprehension in combination with join method in order to achieve what you want.



    import numpy as np
    transpose = np.array(grid).transpose()
    print('n'.join([''.join(row) for row in transpose]))


    Output



    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....





    share|improve this answer















    If you look at your desired output, you'll observe that final matrix is transpose of your given list.



    The transpose of a matrix could be found by using numpy transpose function.



    You could use a list comprehension in combination with join method in order to achieve what you want.



    import numpy as np
    transpose = np.array(grid).transpose()
    print('n'.join([''.join(row) for row in transpose]))


    Output



    ..00.00..
    .0000000.
    .0000000.
    ..00000..
    ...000...
    ....0....






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 8:52

























    answered Nov 14 '18 at 8:41









    Mihai Alexandru-IonutMihai Alexandru-Ionut

    30.3k63971




    30.3k63971













    • I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

      – Mexen
      Nov 14 '18 at 13:52











    • @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

      – Mihai Alexandru-Ionut
      Nov 14 '18 at 14:49



















    • I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

      – Mexen
      Nov 14 '18 at 13:52











    • @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

      – Mihai Alexandru-Ionut
      Nov 14 '18 at 14:49

















    I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

    – Mexen
    Nov 14 '18 at 13:52





    I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.

    – Mexen
    Nov 14 '18 at 13:52













    @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

    – Mihai Alexandru-Ionut
    Nov 14 '18 at 14:49





    @Mexen, you're welcome. Don't forget to vote answers in order to help other people.

    – Mihai Alexandru-Ionut
    Nov 14 '18 at 14:49











    1














    If you really must get the length of a list in a list, just use len(grid[index]):



    for i in range(len(grid)):
    for j in range(len(grid[i])):
    print(mylist[i][j], end='')
    print()


    However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use



    for row in mylist:
    for element in row:
    print(element, end='')
    print()





    share|improve this answer




























      1














      If you really must get the length of a list in a list, just use len(grid[index]):



      for i in range(len(grid)):
      for j in range(len(grid[i])):
      print(mylist[i][j], end='')
      print()


      However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use



      for row in mylist:
      for element in row:
      print(element, end='')
      print()





      share|improve this answer


























        1












        1








        1







        If you really must get the length of a list in a list, just use len(grid[index]):



        for i in range(len(grid)):
        for j in range(len(grid[i])):
        print(mylist[i][j], end='')
        print()


        However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use



        for row in mylist:
        for element in row:
        print(element, end='')
        print()





        share|improve this answer













        If you really must get the length of a list in a list, just use len(grid[index]):



        for i in range(len(grid)):
        for j in range(len(grid[i])):
        print(mylist[i][j], end='')
        print()


        However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use



        for row in mylist:
        for element in row:
        print(element, end='')
        print()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 21:58









        Tomothy32Tomothy32

        5,9971426




        5,9971426























            1














            Another way of doing this is to start by transposing the grid, then just loop over the elements of each row



             grid = [['.', '.', '.', '.', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['0', '0', '0', '0', '0', '.'],
            ['.', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['.', '.', '.', '.', '.', '.']]
            grid2=zip(*grid)
            # print(grid2)
            for row in grid2:
            for el in row:
            print(el, end='')
            print('n')





            share|improve this answer





















            • 1





              You don't actually need to convert grid2 into a list, since you can iterate over it.

              – vishes_shell
              Nov 14 '18 at 8:45











            • Very true, editing

              – robotHamster
              Nov 14 '18 at 8:47
















            1














            Another way of doing this is to start by transposing the grid, then just loop over the elements of each row



             grid = [['.', '.', '.', '.', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['0', '0', '0', '0', '0', '.'],
            ['.', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['.', '.', '.', '.', '.', '.']]
            grid2=zip(*grid)
            # print(grid2)
            for row in grid2:
            for el in row:
            print(el, end='')
            print('n')





            share|improve this answer





















            • 1





              You don't actually need to convert grid2 into a list, since you can iterate over it.

              – vishes_shell
              Nov 14 '18 at 8:45











            • Very true, editing

              – robotHamster
              Nov 14 '18 at 8:47














            1












            1








            1







            Another way of doing this is to start by transposing the grid, then just loop over the elements of each row



             grid = [['.', '.', '.', '.', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['0', '0', '0', '0', '0', '.'],
            ['.', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['.', '.', '.', '.', '.', '.']]
            grid2=zip(*grid)
            # print(grid2)
            for row in grid2:
            for el in row:
            print(el, end='')
            print('n')





            share|improve this answer















            Another way of doing this is to start by transposing the grid, then just loop over the elements of each row



             grid = [['.', '.', '.', '.', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['0', '0', '0', '0', '0', '.'],
            ['.', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '.'],
            ['0', '0', '0', '0', '.', '.'],
            ['.', '0', '0', '.', '.', '.'],
            ['.', '.', '.', '.', '.', '.']]
            grid2=zip(*grid)
            # print(grid2)
            for row in grid2:
            for el in row:
            print(el, end='')
            print('n')






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 7:52

























            answered Nov 14 '18 at 8:36









            robotHamsterrobotHamster

            343216




            343216








            • 1





              You don't actually need to convert grid2 into a list, since you can iterate over it.

              – vishes_shell
              Nov 14 '18 at 8:45











            • Very true, editing

              – robotHamster
              Nov 14 '18 at 8:47














            • 1





              You don't actually need to convert grid2 into a list, since you can iterate over it.

              – vishes_shell
              Nov 14 '18 at 8:45











            • Very true, editing

              – robotHamster
              Nov 14 '18 at 8:47








            1




            1





            You don't actually need to convert grid2 into a list, since you can iterate over it.

            – vishes_shell
            Nov 14 '18 at 8:45





            You don't actually need to convert grid2 into a list, since you can iterate over it.

            – vishes_shell
            Nov 14 '18 at 8:45













            Very true, editing

            – robotHamster
            Nov 14 '18 at 8:47





            Very true, editing

            – robotHamster
            Nov 14 '18 at 8:47


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295848%2fpyhton-retrieve-the-length-of-a-list-value-thats-within-a-list-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues