How to get the longest string length from an array of lists from each lists matching index?
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
add a comment |
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
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 wantlength2 = 7
as'herself'
has 7 characters.
– jpp
Nov 20 '18 at 14:50
add a comment |
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
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
python string list
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 wantlength2 = 7
as'herself'
has 7 characters.
– jpp
Nov 20 '18 at 14:50
add a comment |
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 wantlength2 = 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
add a comment |
3 Answers
3
active
oldest
votes
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}
add a comment |
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
.
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}
add a comment |
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}
add a comment |
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}
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}
edited Nov 20 '18 at 15:25
answered Nov 20 '18 at 14:45


jppjpp
97.7k2159109
97.7k2159109
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Nov 20 '18 at 14:43
bipllbipll
8,0061925
8,0061925
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 20 '18 at 14:57
answered Nov 20 '18 at 14:40


RoadRunnerRoadRunner
11.2k31340
11.2k31340
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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