How to get the longest string length from an array of lists from each lists matching index?












-1















I have an array of lists in the form



list = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]


I want to compare the length of list[0][0] to list[1][0] and list[2][0], basically all the first indexes, and obtain the length of the longest string size.



it must iterate through the list because the number of items and number of lists in the list can be any size.



for example, the answer of this should be



length1 = 5
length2 = 6 #('herself' is longer than 'hi' and 'when')
length3 = 10


TIA!










share|improve this question

























  • Please include your code that is not producing the desired output

    – dfundako
    Nov 20 '18 at 14:37











  • Please show the code you've tried so far and the results you got.

    – Owen
    Nov 20 '18 at 14:37











  • I think you want length2 = 7 as 'herself' has 7 characters.

    – jpp
    Nov 20 '18 at 14:50
















-1















I have an array of lists in the form



list = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]


I want to compare the length of list[0][0] to list[1][0] and list[2][0], basically all the first indexes, and obtain the length of the longest string size.



it must iterate through the list because the number of items and number of lists in the list can be any size.



for example, the answer of this should be



length1 = 5
length2 = 6 #('herself' is longer than 'hi' and 'when')
length3 = 10


TIA!










share|improve this question

























  • Please include your code that is not producing the desired output

    – dfundako
    Nov 20 '18 at 14:37











  • Please show the code you've tried so far and the results you got.

    – Owen
    Nov 20 '18 at 14:37











  • I think you want length2 = 7 as 'herself' has 7 characters.

    – jpp
    Nov 20 '18 at 14:50














-1












-1








-1








I have an array of lists in the form



list = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]


I want to compare the length of list[0][0] to list[1][0] and list[2][0], basically all the first indexes, and obtain the length of the longest string size.



it must iterate through the list because the number of items and number of lists in the list can be any size.



for example, the answer of this should be



length1 = 5
length2 = 6 #('herself' is longer than 'hi' and 'when')
length3 = 10


TIA!










share|improve this question
















I have an array of lists in the form



list = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]


I want to compare the length of list[0][0] to list[1][0] and list[2][0], basically all the first indexes, and obtain the length of the longest string size.



it must iterate through the list because the number of items and number of lists in the list can be any size.



for example, the answer of this should be



length1 = 5
length2 = 6 #('herself' is longer than 'hi' and 'when')
length3 = 10


TIA!







python string list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 14:50









jpp

97.7k2159109




97.7k2159109










asked Nov 20 '18 at 14:35









Python newbiePython newbie

144




144













  • Please include your code that is not producing the desired output

    – dfundako
    Nov 20 '18 at 14:37











  • Please show the code you've tried so far and the results you got.

    – Owen
    Nov 20 '18 at 14:37











  • I think you want length2 = 7 as 'herself' has 7 characters.

    – jpp
    Nov 20 '18 at 14:50



















  • Please include your code that is not producing the desired output

    – dfundako
    Nov 20 '18 at 14:37











  • Please show the code you've tried so far and the results you got.

    – Owen
    Nov 20 '18 at 14:37











  • I think you want length2 = 7 as 'herself' has 7 characters.

    – jpp
    Nov 20 '18 at 14:50

















Please include your code that is not producing the desired output

– dfundako
Nov 20 '18 at 14:37





Please include your code that is not producing the desired output

– dfundako
Nov 20 '18 at 14:37













Please show the code you've tried so far and the results you got.

– Owen
Nov 20 '18 at 14:37





Please show the code you've tried so far and the results you got.

– Owen
Nov 20 '18 at 14:37













I think you want length2 = 7 as 'herself' has 7 characters.

– jpp
Nov 20 '18 at 14:50





I think you want length2 = 7 as 'herself' has 7 characters.

– jpp
Nov 20 '18 at 14:50












3 Answers
3






active

oldest

votes


















1














You don't need to create a variable number of variables. You can use either a list comprehension or a dictionary:



L = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

# list comprehension
res_list = [max(map(len, i)) for i in zip(*L)]

[5, 7, 10]

# dictionary from enumerated generator expression
res_dict = dict(enumerate((max(map(len, i)) for i in zip(*L)), 1))

{1: 5, 2: 7, 3: 10}





share|improve this answer

































    0














    Lots of ways to do it in Python.



    array = [['hello','hi','hey'],
    ['where','when','why'],
    ['him','herself','themselves']]

    length1 = 0
    for elem in array:
    if length1 < len(elem[0]):
    length1 = len(elem[0])

    length2 = max(array, key=lambda elem: len(elem[1]))

    from itertools import accumulate
    length3 = accumulate(array,
    lambda e1, e2: max(len(e1[2]), len(e2[2]))


    As a side note it is generally not recommended to assign something to standard identifiers, like list.






    share|improve this answer































      0














      Just go through the triples from zip() and print out the length of the longest word:



      lst = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

      for i, triple in enumerate(zip(*lst), start=1):
      print('length%d = %d' % (i, len(max(triple, key=len))))

      # length1 = 5
      # length2 = 7
      # length3 = 10


      Or as a dictionary:



      {'length%d' % i: len(max(e, key=len)) for i, e in enumerate(zip(*lst), start=1)}
      # {'length1': 5, 'length2': 7, 'length3': 10}


      Which is nicer than storing variables for each length.






      share|improve this answer

























        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%2f53395343%2fhow-to-get-the-longest-string-length-from-an-array-of-lists-from-each-lists-matc%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        You don't need to create a variable number of variables. You can use either a list comprehension or a dictionary:



        L = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

        # list comprehension
        res_list = [max(map(len, i)) for i in zip(*L)]

        [5, 7, 10]

        # dictionary from enumerated generator expression
        res_dict = dict(enumerate((max(map(len, i)) for i in zip(*L)), 1))

        {1: 5, 2: 7, 3: 10}





        share|improve this answer






























          1














          You don't need to create a variable number of variables. You can use either a list comprehension or a dictionary:



          L = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

          # list comprehension
          res_list = [max(map(len, i)) for i in zip(*L)]

          [5, 7, 10]

          # dictionary from enumerated generator expression
          res_dict = dict(enumerate((max(map(len, i)) for i in zip(*L)), 1))

          {1: 5, 2: 7, 3: 10}





          share|improve this answer




























            1












            1








            1







            You don't need to create a variable number of variables. You can use either a list comprehension or a dictionary:



            L = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

            # list comprehension
            res_list = [max(map(len, i)) for i in zip(*L)]

            [5, 7, 10]

            # dictionary from enumerated generator expression
            res_dict = dict(enumerate((max(map(len, i)) for i in zip(*L)), 1))

            {1: 5, 2: 7, 3: 10}





            share|improve this answer















            You don't need to create a variable number of variables. You can use either a list comprehension or a dictionary:



            L = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

            # list comprehension
            res_list = [max(map(len, i)) for i in zip(*L)]

            [5, 7, 10]

            # dictionary from enumerated generator expression
            res_dict = dict(enumerate((max(map(len, i)) for i in zip(*L)), 1))

            {1: 5, 2: 7, 3: 10}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 15:25

























            answered Nov 20 '18 at 14:45









            jppjpp

            97.7k2159109




            97.7k2159109

























                0














                Lots of ways to do it in Python.



                array = [['hello','hi','hey'],
                ['where','when','why'],
                ['him','herself','themselves']]

                length1 = 0
                for elem in array:
                if length1 < len(elem[0]):
                length1 = len(elem[0])

                length2 = max(array, key=lambda elem: len(elem[1]))

                from itertools import accumulate
                length3 = accumulate(array,
                lambda e1, e2: max(len(e1[2]), len(e2[2]))


                As a side note it is generally not recommended to assign something to standard identifiers, like list.






                share|improve this answer




























                  0














                  Lots of ways to do it in Python.



                  array = [['hello','hi','hey'],
                  ['where','when','why'],
                  ['him','herself','themselves']]

                  length1 = 0
                  for elem in array:
                  if length1 < len(elem[0]):
                  length1 = len(elem[0])

                  length2 = max(array, key=lambda elem: len(elem[1]))

                  from itertools import accumulate
                  length3 = accumulate(array,
                  lambda e1, e2: max(len(e1[2]), len(e2[2]))


                  As a side note it is generally not recommended to assign something to standard identifiers, like list.






                  share|improve this answer


























                    0












                    0








                    0







                    Lots of ways to do it in Python.



                    array = [['hello','hi','hey'],
                    ['where','when','why'],
                    ['him','herself','themselves']]

                    length1 = 0
                    for elem in array:
                    if length1 < len(elem[0]):
                    length1 = len(elem[0])

                    length2 = max(array, key=lambda elem: len(elem[1]))

                    from itertools import accumulate
                    length3 = accumulate(array,
                    lambda e1, e2: max(len(e1[2]), len(e2[2]))


                    As a side note it is generally not recommended to assign something to standard identifiers, like list.






                    share|improve this answer













                    Lots of ways to do it in Python.



                    array = [['hello','hi','hey'],
                    ['where','when','why'],
                    ['him','herself','themselves']]

                    length1 = 0
                    for elem in array:
                    if length1 < len(elem[0]):
                    length1 = len(elem[0])

                    length2 = max(array, key=lambda elem: len(elem[1]))

                    from itertools import accumulate
                    length3 = accumulate(array,
                    lambda e1, e2: max(len(e1[2]), len(e2[2]))


                    As a side note it is generally not recommended to assign something to standard identifiers, like list.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 14:43









                    bipllbipll

                    8,0061925




                    8,0061925























                        0














                        Just go through the triples from zip() and print out the length of the longest word:



                        lst = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

                        for i, triple in enumerate(zip(*lst), start=1):
                        print('length%d = %d' % (i, len(max(triple, key=len))))

                        # length1 = 5
                        # length2 = 7
                        # length3 = 10


                        Or as a dictionary:



                        {'length%d' % i: len(max(e, key=len)) for i, e in enumerate(zip(*lst), start=1)}
                        # {'length1': 5, 'length2': 7, 'length3': 10}


                        Which is nicer than storing variables for each length.






                        share|improve this answer






























                          0














                          Just go through the triples from zip() and print out the length of the longest word:



                          lst = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

                          for i, triple in enumerate(zip(*lst), start=1):
                          print('length%d = %d' % (i, len(max(triple, key=len))))

                          # length1 = 5
                          # length2 = 7
                          # length3 = 10


                          Or as a dictionary:



                          {'length%d' % i: len(max(e, key=len)) for i, e in enumerate(zip(*lst), start=1)}
                          # {'length1': 5, 'length2': 7, 'length3': 10}


                          Which is nicer than storing variables for each length.






                          share|improve this answer




























                            0












                            0








                            0







                            Just go through the triples from zip() and print out the length of the longest word:



                            lst = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

                            for i, triple in enumerate(zip(*lst), start=1):
                            print('length%d = %d' % (i, len(max(triple, key=len))))

                            # length1 = 5
                            # length2 = 7
                            # length3 = 10


                            Or as a dictionary:



                            {'length%d' % i: len(max(e, key=len)) for i, e in enumerate(zip(*lst), start=1)}
                            # {'length1': 5, 'length2': 7, 'length3': 10}


                            Which is nicer than storing variables for each length.






                            share|improve this answer















                            Just go through the triples from zip() and print out the length of the longest word:



                            lst = [['hello','hi','hey'],['where','when','why'],['him','herself','themselves']]

                            for i, triple in enumerate(zip(*lst), start=1):
                            print('length%d = %d' % (i, len(max(triple, key=len))))

                            # length1 = 5
                            # length2 = 7
                            # length3 = 10


                            Or as a dictionary:



                            {'length%d' % i: len(max(e, key=len)) for i, e in enumerate(zip(*lst), start=1)}
                            # {'length1': 5, 'length2': 7, 'length3': 10}


                            Which is nicer than storing variables for each length.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 20 '18 at 14:57

























                            answered Nov 20 '18 at 14:40









                            RoadRunnerRoadRunner

                            11.2k31340




                            11.2k31340






























                                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%2f53395343%2fhow-to-get-the-longest-string-length-from-an-array-of-lists-from-each-lists-matc%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

                                MongoDB - Not Authorized To Execute Command

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                How to fix TextFormField cause rebuild widget in Flutter