Making a new dictionary using the key and value with the longest length from the old one, how to increment a...












1















So, I have a dictionary with a bunch of keys and values, e.g.



dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}


Each key has at least 2 values in my dictionary, hence maxlen = 2 below.



I want to make a new dictionary 'longest' that gets the key and its value that has the most amount of items in it's list, so 'arst' in this case.



I wrote this:



maxlen = 2

longest = {k: v for k, v in dictionary.items() if (len(v) > maxlen)}


The code above will do what I want it to if I manage to set maxlen = len(v) each time len(v) > maxlen (just like finding the maximum normally of a list or something) but how do I do that here?



So, at this part:



if (len(v) > maxlen)


I want this to happen:



len(v) = maxlen


How do I do that and can I fit that in that one line without writing a bunch of different lines or loops?



Thanks










share|improve this question




















  • 2





    Can you share the expected output?

    – timgeb
    Nov 25 '18 at 17:38











  • @timgeb It'd just be the key and its values that has the longest length, so longest = {'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']} in this case.

    – Mandingo
    Nov 25 '18 at 17:41






  • 1





    What if two keys have the same amount of elements? Do you want both in this case?

    – Felix
    Nov 25 '18 at 17:43






  • 1





    @Felix Yes, both.

    – Mandingo
    Nov 25 '18 at 17:45






  • 1





    Ok then @timgeb posted the answer already

    – Felix
    Nov 25 '18 at 17:47
















1















So, I have a dictionary with a bunch of keys and values, e.g.



dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}


Each key has at least 2 values in my dictionary, hence maxlen = 2 below.



I want to make a new dictionary 'longest' that gets the key and its value that has the most amount of items in it's list, so 'arst' in this case.



I wrote this:



maxlen = 2

longest = {k: v for k, v in dictionary.items() if (len(v) > maxlen)}


The code above will do what I want it to if I manage to set maxlen = len(v) each time len(v) > maxlen (just like finding the maximum normally of a list or something) but how do I do that here?



So, at this part:



if (len(v) > maxlen)


I want this to happen:



len(v) = maxlen


How do I do that and can I fit that in that one line without writing a bunch of different lines or loops?



Thanks










share|improve this question




















  • 2





    Can you share the expected output?

    – timgeb
    Nov 25 '18 at 17:38











  • @timgeb It'd just be the key and its values that has the longest length, so longest = {'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']} in this case.

    – Mandingo
    Nov 25 '18 at 17:41






  • 1





    What if two keys have the same amount of elements? Do you want both in this case?

    – Felix
    Nov 25 '18 at 17:43






  • 1





    @Felix Yes, both.

    – Mandingo
    Nov 25 '18 at 17:45






  • 1





    Ok then @timgeb posted the answer already

    – Felix
    Nov 25 '18 at 17:47














1












1








1








So, I have a dictionary with a bunch of keys and values, e.g.



dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}


Each key has at least 2 values in my dictionary, hence maxlen = 2 below.



I want to make a new dictionary 'longest' that gets the key and its value that has the most amount of items in it's list, so 'arst' in this case.



I wrote this:



maxlen = 2

longest = {k: v for k, v in dictionary.items() if (len(v) > maxlen)}


The code above will do what I want it to if I manage to set maxlen = len(v) each time len(v) > maxlen (just like finding the maximum normally of a list or something) but how do I do that here?



So, at this part:



if (len(v) > maxlen)


I want this to happen:



len(v) = maxlen


How do I do that and can I fit that in that one line without writing a bunch of different lines or loops?



Thanks










share|improve this question
















So, I have a dictionary with a bunch of keys and values, e.g.



dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}


Each key has at least 2 values in my dictionary, hence maxlen = 2 below.



I want to make a new dictionary 'longest' that gets the key and its value that has the most amount of items in it's list, so 'arst' in this case.



I wrote this:



maxlen = 2

longest = {k: v for k, v in dictionary.items() if (len(v) > maxlen)}


The code above will do what I want it to if I manage to set maxlen = len(v) each time len(v) > maxlen (just like finding the maximum normally of a list or something) but how do I do that here?



So, at this part:



if (len(v) > maxlen)


I want this to happen:



len(v) = maxlen


How do I do that and can I fit that in that one line without writing a bunch of different lines or loops?



Thanks







python dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 17:41







Mandingo

















asked Nov 25 '18 at 17:37









MandingoMandingo

376212




376212








  • 2





    Can you share the expected output?

    – timgeb
    Nov 25 '18 at 17:38











  • @timgeb It'd just be the key and its values that has the longest length, so longest = {'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']} in this case.

    – Mandingo
    Nov 25 '18 at 17:41






  • 1





    What if two keys have the same amount of elements? Do you want both in this case?

    – Felix
    Nov 25 '18 at 17:43






  • 1





    @Felix Yes, both.

    – Mandingo
    Nov 25 '18 at 17:45






  • 1





    Ok then @timgeb posted the answer already

    – Felix
    Nov 25 '18 at 17:47














  • 2





    Can you share the expected output?

    – timgeb
    Nov 25 '18 at 17:38











  • @timgeb It'd just be the key and its values that has the longest length, so longest = {'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']} in this case.

    – Mandingo
    Nov 25 '18 at 17:41






  • 1





    What if two keys have the same amount of elements? Do you want both in this case?

    – Felix
    Nov 25 '18 at 17:43






  • 1





    @Felix Yes, both.

    – Mandingo
    Nov 25 '18 at 17:45






  • 1





    Ok then @timgeb posted the answer already

    – Felix
    Nov 25 '18 at 17:47








2




2





Can you share the expected output?

– timgeb
Nov 25 '18 at 17:38





Can you share the expected output?

– timgeb
Nov 25 '18 at 17:38













@timgeb It'd just be the key and its values that has the longest length, so longest = {'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']} in this case.

– Mandingo
Nov 25 '18 at 17:41





@timgeb It'd just be the key and its values that has the longest length, so longest = {'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']} in this case.

– Mandingo
Nov 25 '18 at 17:41




1




1





What if two keys have the same amount of elements? Do you want both in this case?

– Felix
Nov 25 '18 at 17:43





What if two keys have the same amount of elements? Do you want both in this case?

– Felix
Nov 25 '18 at 17:43




1




1





@Felix Yes, both.

– Mandingo
Nov 25 '18 at 17:45





@Felix Yes, both.

– Mandingo
Nov 25 '18 at 17:45




1




1





Ok then @timgeb posted the answer already

– Felix
Nov 25 '18 at 17:47





Ok then @timgeb posted the answer already

– Felix
Nov 25 '18 at 17:47












1 Answer
1






active

oldest

votes


















3














Find the length of the longest values first



>>> dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}
>>> max_len = len(max(dictionary.values(), key=len)) # 7
>>> max_len
7


then filter your dictionary.



>>> {k:v for k,v in dictionary.items() if len(v) == max_len}
{'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}





share|improve this answer
























  • Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

    – Mandingo
    Nov 25 '18 at 17:44






  • 1





    @Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

    – timgeb
    Nov 25 '18 at 17:46






  • 2





    I like max_len2 = max(map(len, dictionary.values())) even better ;-)

    – Felix
    Nov 25 '18 at 17:47











  • Thanks, it works! :)

    – Mandingo
    Nov 25 '18 at 17:50






  • 1





    @Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

    – timgeb
    Nov 25 '18 at 17:53













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%2f53470123%2fmaking-a-new-dictionary-using-the-key-and-value-with-the-longest-length-from-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Find the length of the longest values first



>>> dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}
>>> max_len = len(max(dictionary.values(), key=len)) # 7
>>> max_len
7


then filter your dictionary.



>>> {k:v for k,v in dictionary.items() if len(v) == max_len}
{'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}





share|improve this answer
























  • Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

    – Mandingo
    Nov 25 '18 at 17:44






  • 1





    @Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

    – timgeb
    Nov 25 '18 at 17:46






  • 2





    I like max_len2 = max(map(len, dictionary.values())) even better ;-)

    – Felix
    Nov 25 '18 at 17:47











  • Thanks, it works! :)

    – Mandingo
    Nov 25 '18 at 17:50






  • 1





    @Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

    – timgeb
    Nov 25 '18 at 17:53


















3














Find the length of the longest values first



>>> dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}
>>> max_len = len(max(dictionary.values(), key=len)) # 7
>>> max_len
7


then filter your dictionary.



>>> {k:v for k,v in dictionary.items() if len(v) == max_len}
{'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}





share|improve this answer
























  • Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

    – Mandingo
    Nov 25 '18 at 17:44






  • 1





    @Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

    – timgeb
    Nov 25 '18 at 17:46






  • 2





    I like max_len2 = max(map(len, dictionary.values())) even better ;-)

    – Felix
    Nov 25 '18 at 17:47











  • Thanks, it works! :)

    – Mandingo
    Nov 25 '18 at 17:50






  • 1





    @Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

    – timgeb
    Nov 25 '18 at 17:53
















3












3








3







Find the length of the longest values first



>>> dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}
>>> max_len = len(max(dictionary.values(), key=len)) # 7
>>> max_len
7


then filter your dictionary.



>>> {k:v for k,v in dictionary.items() if len(v) == max_len}
{'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}





share|improve this answer













Find the length of the longest values first



>>> dictionary = {'aety': ['yate', 'yeat', 'yeta'], 'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}
>>> max_len = len(max(dictionary.values(), key=len)) # 7
>>> max_len
7


then filter your dictionary.



>>> {k:v for k,v in dictionary.items() if len(v) == max_len}
{'arst': ['sart', 'sart', 'star', 'star', 'stra', 'tars', 'tsar']}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 17:40









timgebtimgeb

51.2k116794




51.2k116794













  • Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

    – Mandingo
    Nov 25 '18 at 17:44






  • 1





    @Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

    – timgeb
    Nov 25 '18 at 17:46






  • 2





    I like max_len2 = max(map(len, dictionary.values())) even better ;-)

    – Felix
    Nov 25 '18 at 17:47











  • Thanks, it works! :)

    – Mandingo
    Nov 25 '18 at 17:50






  • 1





    @Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

    – timgeb
    Nov 25 '18 at 17:53





















  • Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

    – Mandingo
    Nov 25 '18 at 17:44






  • 1





    @Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

    – timgeb
    Nov 25 '18 at 17:46






  • 2





    I like max_len2 = max(map(len, dictionary.values())) even better ;-)

    – Felix
    Nov 25 '18 at 17:47











  • Thanks, it works! :)

    – Mandingo
    Nov 25 '18 at 17:50






  • 1





    @Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

    – timgeb
    Nov 25 '18 at 17:53



















Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

– Mandingo
Nov 25 '18 at 17:44





Thanks. Does this change my current dictionary? As in, can I still print out the old dictionary with all the old values before finding the maximum length and using that?

– Mandingo
Nov 25 '18 at 17:44




1




1





@Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

– timgeb
Nov 25 '18 at 17:46





@Mandingo your old dictionary is not mutated, I'm building a new dictionary with the result here.

– timgeb
Nov 25 '18 at 17:46




2




2





I like max_len2 = max(map(len, dictionary.values())) even better ;-)

– Felix
Nov 25 '18 at 17:47





I like max_len2 = max(map(len, dictionary.values())) even better ;-)

– Felix
Nov 25 '18 at 17:47













Thanks, it works! :)

– Mandingo
Nov 25 '18 at 17:50





Thanks, it works! :)

– Mandingo
Nov 25 '18 at 17:50




1




1





@Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

– timgeb
Nov 25 '18 at 17:53







@Felix I like that too. As far as performance goes, I timed it and there does not seem to be any noticeable difference.

– timgeb
Nov 25 '18 at 17:53






















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%2f53470123%2fmaking-a-new-dictionary-using-the-key-and-value-with-the-longest-length-from-the%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

Wiesbaden

To store a contact into the json file from server.js file using a class in NodeJS

Marschland