Finding all the dicts of max len in a list of dicts in Python
I have a list of dictionaries
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
I need to get all the elements with the longest length from my list, i.e.
{'b':2, 'c':3}` and `{'d':4, 'e':5}
I'm not very knowledgeable in Python but I found that:
>>> max(ld, key=len)
{'b': 2, 'c': 3}`
And, an even better solution that returns the index of the longest length dictionary:
>>> max(enumerate(ld), key=lambda tup: len(tup[1]))
(1, {'b': 2, 'c': 3})
I would like to use an expression that would return something like
(1: {'b': 2, 'c': 3}, 2: {'d':4, 'e':5})
and I feel like I'm not far from the solution (or maybe I am) but I just don't know how to get it.
python
add a comment |
I have a list of dictionaries
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
I need to get all the elements with the longest length from my list, i.e.
{'b':2, 'c':3}` and `{'d':4, 'e':5}
I'm not very knowledgeable in Python but I found that:
>>> max(ld, key=len)
{'b': 2, 'c': 3}`
And, an even better solution that returns the index of the longest length dictionary:
>>> max(enumerate(ld), key=lambda tup: len(tup[1]))
(1, {'b': 2, 'c': 3})
I would like to use an expression that would return something like
(1: {'b': 2, 'c': 3}, 2: {'d':4, 'e':5})
and I feel like I'm not far from the solution (or maybe I am) but I just don't know how to get it.
python
add a comment |
I have a list of dictionaries
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
I need to get all the elements with the longest length from my list, i.e.
{'b':2, 'c':3}` and `{'d':4, 'e':5}
I'm not very knowledgeable in Python but I found that:
>>> max(ld, key=len)
{'b': 2, 'c': 3}`
And, an even better solution that returns the index of the longest length dictionary:
>>> max(enumerate(ld), key=lambda tup: len(tup[1]))
(1, {'b': 2, 'c': 3})
I would like to use an expression that would return something like
(1: {'b': 2, 'c': 3}, 2: {'d':4, 'e':5})
and I feel like I'm not far from the solution (or maybe I am) but I just don't know how to get it.
python
I have a list of dictionaries
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
I need to get all the elements with the longest length from my list, i.e.
{'b':2, 'c':3}` and `{'d':4, 'e':5}
I'm not very knowledgeable in Python but I found that:
>>> max(ld, key=len)
{'b': 2, 'c': 3}`
And, an even better solution that returns the index of the longest length dictionary:
>>> max(enumerate(ld), key=lambda tup: len(tup[1]))
(1, {'b': 2, 'c': 3})
I would like to use an expression that would return something like
(1: {'b': 2, 'c': 3}, 2: {'d':4, 'e':5})
and I feel like I'm not far from the solution (or maybe I am) but I just don't know how to get it.
python
python
edited Dec 14 at 0:34
Jayjayyy
1,70011726
1,70011726
asked Dec 13 at 23:45
Ricardo Jesus
387
387
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can find the length of the maximum dictionary in the structure, and then use a list comprehension:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
_max = max(map(len, ld))
new_result = dict(i for i in enumerate(ld) if len(i[-1]) == _max)
Output:
{1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
add a comment |
Ajax1234 provided a really good solution. If you want something of a beginner level, here's a solution.
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
ans = dict()
for value in ld:
if len(value) in ans:
ans[len(value)].append(value)
else:
ans[len(value)] = list()
ans[len(value)].append(value)
ans[max(ans)]
Basically, you add everything in a dictionary to get the maximum dictionary size to be the key, and dictionary list to be the value, and then get that maximum size list of dictionaries.
1
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
add a comment |
There are a number of ways you could do this in python. Here's one example which illustrates a few different python capabilities:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
lengths = list(map(len, ld)) # [1, 2, 2]
max_len = max(lengths) # 2
index_to_max_length_dictionaries = {
index: dictionary
for index, dictionary in enumerate(ld)
if len(dictionary) == max_len
}
# output: {1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
add a comment |
Find the maximum length and then use a dictionary comprehension to find the dictionaries with such length
max_l = len(max(ld, key=len))
result = {i: d for i, d in enumerate(ld) if len(d) == max_l}
This is the simplest and more readable approach you can take
Below is another path, a better (but more verbose) approach
max_length = 0
result = dict()
for i, d in enumerate(ld):
l = len(d)
if l == max_length:
result[i] = d
elif l > max_length:
max_length = l
result = {i: d}
This is the most efficient approach. It just iterate 1 time through the full input list
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
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%2f53771668%2ffinding-all-the-dicts-of-max-len-in-a-list-of-dicts-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can find the length of the maximum dictionary in the structure, and then use a list comprehension:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
_max = max(map(len, ld))
new_result = dict(i for i in enumerate(ld) if len(i[-1]) == _max)
Output:
{1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
add a comment |
You can find the length of the maximum dictionary in the structure, and then use a list comprehension:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
_max = max(map(len, ld))
new_result = dict(i for i in enumerate(ld) if len(i[-1]) == _max)
Output:
{1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
add a comment |
You can find the length of the maximum dictionary in the structure, and then use a list comprehension:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
_max = max(map(len, ld))
new_result = dict(i for i in enumerate(ld) if len(i[-1]) == _max)
Output:
{1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
You can find the length of the maximum dictionary in the structure, and then use a list comprehension:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
_max = max(map(len, ld))
new_result = dict(i for i in enumerate(ld) if len(i[-1]) == _max)
Output:
{1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
answered Dec 13 at 23:52
Ajax1234
39.8k42652
39.8k42652
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
add a comment |
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
thank you for the fast reply!
– Ricardo Jesus
Dec 14 at 16:15
add a comment |
Ajax1234 provided a really good solution. If you want something of a beginner level, here's a solution.
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
ans = dict()
for value in ld:
if len(value) in ans:
ans[len(value)].append(value)
else:
ans[len(value)] = list()
ans[len(value)].append(value)
ans[max(ans)]
Basically, you add everything in a dictionary to get the maximum dictionary size to be the key, and dictionary list to be the value, and then get that maximum size list of dictionaries.
1
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
add a comment |
Ajax1234 provided a really good solution. If you want something of a beginner level, here's a solution.
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
ans = dict()
for value in ld:
if len(value) in ans:
ans[len(value)].append(value)
else:
ans[len(value)] = list()
ans[len(value)].append(value)
ans[max(ans)]
Basically, you add everything in a dictionary to get the maximum dictionary size to be the key, and dictionary list to be the value, and then get that maximum size list of dictionaries.
1
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
add a comment |
Ajax1234 provided a really good solution. If you want something of a beginner level, here's a solution.
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
ans = dict()
for value in ld:
if len(value) in ans:
ans[len(value)].append(value)
else:
ans[len(value)] = list()
ans[len(value)].append(value)
ans[max(ans)]
Basically, you add everything in a dictionary to get the maximum dictionary size to be the key, and dictionary list to be the value, and then get that maximum size list of dictionaries.
Ajax1234 provided a really good solution. If you want something of a beginner level, here's a solution.
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
ans = dict()
for value in ld:
if len(value) in ans:
ans[len(value)].append(value)
else:
ans[len(value)] = list()
ans[len(value)].append(value)
ans[max(ans)]
Basically, you add everything in a dictionary to get the maximum dictionary size to be the key, and dictionary list to be the value, and then get that maximum size list of dictionaries.
answered Dec 13 at 23:59
kokeen
8117
8117
1
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
add a comment |
1
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
1
1
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
Thank you for the ELI5 response! Appreciate it.
– Ricardo Jesus
Dec 14 at 16:16
add a comment |
There are a number of ways you could do this in python. Here's one example which illustrates a few different python capabilities:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
lengths = list(map(len, ld)) # [1, 2, 2]
max_len = max(lengths) # 2
index_to_max_length_dictionaries = {
index: dictionary
for index, dictionary in enumerate(ld)
if len(dictionary) == max_len
}
# output: {1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
add a comment |
There are a number of ways you could do this in python. Here's one example which illustrates a few different python capabilities:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
lengths = list(map(len, ld)) # [1, 2, 2]
max_len = max(lengths) # 2
index_to_max_length_dictionaries = {
index: dictionary
for index, dictionary in enumerate(ld)
if len(dictionary) == max_len
}
# output: {1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
add a comment |
There are a number of ways you could do this in python. Here's one example which illustrates a few different python capabilities:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
lengths = list(map(len, ld)) # [1, 2, 2]
max_len = max(lengths) # 2
index_to_max_length_dictionaries = {
index: dictionary
for index, dictionary in enumerate(ld)
if len(dictionary) == max_len
}
# output: {1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
There are a number of ways you could do this in python. Here's one example which illustrates a few different python capabilities:
ld = [{'a':1}, {'b':2, 'c':3}, {'d':4, 'e':5}]
lengths = list(map(len, ld)) # [1, 2, 2]
max_len = max(lengths) # 2
index_to_max_length_dictionaries = {
index: dictionary
for index, dictionary in enumerate(ld)
if len(dictionary) == max_len
}
# output: {1: {'b': 2, 'c': 3}, 2: {'d': 4, 'e': 5}}
answered Dec 13 at 23:58
Jaime M
313
313
add a comment |
add a comment |
Find the maximum length and then use a dictionary comprehension to find the dictionaries with such length
max_l = len(max(ld, key=len))
result = {i: d for i, d in enumerate(ld) if len(d) == max_l}
This is the simplest and more readable approach you can take
Below is another path, a better (but more verbose) approach
max_length = 0
result = dict()
for i, d in enumerate(ld):
l = len(d)
if l == max_length:
result[i] = d
elif l > max_length:
max_length = l
result = {i: d}
This is the most efficient approach. It just iterate 1 time through the full input list
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
add a comment |
Find the maximum length and then use a dictionary comprehension to find the dictionaries with such length
max_l = len(max(ld, key=len))
result = {i: d for i, d in enumerate(ld) if len(d) == max_l}
This is the simplest and more readable approach you can take
Below is another path, a better (but more verbose) approach
max_length = 0
result = dict()
for i, d in enumerate(ld):
l = len(d)
if l == max_length:
result[i] = d
elif l > max_length:
max_length = l
result = {i: d}
This is the most efficient approach. It just iterate 1 time through the full input list
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
add a comment |
Find the maximum length and then use a dictionary comprehension to find the dictionaries with such length
max_l = len(max(ld, key=len))
result = {i: d for i, d in enumerate(ld) if len(d) == max_l}
This is the simplest and more readable approach you can take
Below is another path, a better (but more verbose) approach
max_length = 0
result = dict()
for i, d in enumerate(ld):
l = len(d)
if l == max_length:
result[i] = d
elif l > max_length:
max_length = l
result = {i: d}
This is the most efficient approach. It just iterate 1 time through the full input list
Find the maximum length and then use a dictionary comprehension to find the dictionaries with such length
max_l = len(max(ld, key=len))
result = {i: d for i, d in enumerate(ld) if len(d) == max_l}
This is the simplest and more readable approach you can take
Below is another path, a better (but more verbose) approach
max_length = 0
result = dict()
for i, d in enumerate(ld):
l = len(d)
if l == max_length:
result[i] = d
elif l > max_length:
max_length = l
result = {i: d}
This is the most efficient approach. It just iterate 1 time through the full input list
edited Dec 14 at 0:19
answered Dec 14 at 0:09
Josué Cortina
4738
4738
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
add a comment |
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
Thank you for the in-depth reply!
– Ricardo Jesus
Dec 14 at 16:15
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
No problem, bro! Although it would be nice having my answer marked as the right one. LOL
– Josué Cortina
Dec 14 at 16:36
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53771668%2ffinding-all-the-dicts-of-max-len-in-a-list-of-dicts-in-python%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