Making two lists into a dictionary error: unhashable type: 'list'











up vote
-1
down vote

favorite












Hi I'm trying to make two lists into a dictionary as keys and values, but I got error



TypeError                                 Traceback (most recent call last)
<ipython-input-535-a88b451e7100> in <module>()
1 #
2 DN = {key: value for key, value in zip(NiW, NiV)}
----> 3 DY = {key: value for key, value in zip(YiW, YiV)}
4 D = dict(DN, **DY)
TypeError: unhashable type: 'list'


I did some research it seems like it's the outer list of the nested list caused this error but I'm unsure



The Data



YiW
[['africa', 'trip'],
['asia', 'travel'],
['europe', 'holiday']]

YiV
[[array([-0.34219775, 0.61445 , 0.19807251],
array([ 1.8527551 , 2.4294894 , 0.3062766],
[array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
array([ 1.8527551 , 2.4294894 , 0.3062766],
[array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
array([ 1.8527551 , 2.4294894 , 0.3062766]]


Idea output:



{'africa':array([-0.34219775,  0.61445   ,  0.19807251],
'trip':array([-0.34219775, 0.61445 , 0.19807251, 0.15776388]}etc..


I tried many ways to remove outer list:
flatten-list-of-lists



concatenate-item-in-list-to-strings



how-to-convert-nested-list-into-dictionary-in-python-where-lst00-is-the-key
but they don't work in this occasion, can anyone please help? Thank you in advanced !










share|improve this question


























    up vote
    -1
    down vote

    favorite












    Hi I'm trying to make two lists into a dictionary as keys and values, but I got error



    TypeError                                 Traceback (most recent call last)
    <ipython-input-535-a88b451e7100> in <module>()
    1 #
    2 DN = {key: value for key, value in zip(NiW, NiV)}
    ----> 3 DY = {key: value for key, value in zip(YiW, YiV)}
    4 D = dict(DN, **DY)
    TypeError: unhashable type: 'list'


    I did some research it seems like it's the outer list of the nested list caused this error but I'm unsure



    The Data



    YiW
    [['africa', 'trip'],
    ['asia', 'travel'],
    ['europe', 'holiday']]

    YiV
    [[array([-0.34219775, 0.61445 , 0.19807251],
    array([ 1.8527551 , 2.4294894 , 0.3062766],
    [array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
    array([ 1.8527551 , 2.4294894 , 0.3062766],
    [array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
    array([ 1.8527551 , 2.4294894 , 0.3062766]]


    Idea output:



    {'africa':array([-0.34219775,  0.61445   ,  0.19807251],
    'trip':array([-0.34219775, 0.61445 , 0.19807251, 0.15776388]}etc..


    I tried many ways to remove outer list:
    flatten-list-of-lists



    concatenate-item-in-list-to-strings



    how-to-convert-nested-list-into-dictionary-in-python-where-lst00-is-the-key
    but they don't work in this occasion, can anyone please help? Thank you in advanced !










    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Hi I'm trying to make two lists into a dictionary as keys and values, but I got error



      TypeError                                 Traceback (most recent call last)
      <ipython-input-535-a88b451e7100> in <module>()
      1 #
      2 DN = {key: value for key, value in zip(NiW, NiV)}
      ----> 3 DY = {key: value for key, value in zip(YiW, YiV)}
      4 D = dict(DN, **DY)
      TypeError: unhashable type: 'list'


      I did some research it seems like it's the outer list of the nested list caused this error but I'm unsure



      The Data



      YiW
      [['africa', 'trip'],
      ['asia', 'travel'],
      ['europe', 'holiday']]

      YiV
      [[array([-0.34219775, 0.61445 , 0.19807251],
      array([ 1.8527551 , 2.4294894 , 0.3062766],
      [array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
      array([ 1.8527551 , 2.4294894 , 0.3062766],
      [array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
      array([ 1.8527551 , 2.4294894 , 0.3062766]]


      Idea output:



      {'africa':array([-0.34219775,  0.61445   ,  0.19807251],
      'trip':array([-0.34219775, 0.61445 , 0.19807251, 0.15776388]}etc..


      I tried many ways to remove outer list:
      flatten-list-of-lists



      concatenate-item-in-list-to-strings



      how-to-convert-nested-list-into-dictionary-in-python-where-lst00-is-the-key
      but they don't work in this occasion, can anyone please help? Thank you in advanced !










      share|improve this question













      Hi I'm trying to make two lists into a dictionary as keys and values, but I got error



      TypeError                                 Traceback (most recent call last)
      <ipython-input-535-a88b451e7100> in <module>()
      1 #
      2 DN = {key: value for key, value in zip(NiW, NiV)}
      ----> 3 DY = {key: value for key, value in zip(YiW, YiV)}
      4 D = dict(DN, **DY)
      TypeError: unhashable type: 'list'


      I did some research it seems like it's the outer list of the nested list caused this error but I'm unsure



      The Data



      YiW
      [['africa', 'trip'],
      ['asia', 'travel'],
      ['europe', 'holiday']]

      YiV
      [[array([-0.34219775, 0.61445 , 0.19807251],
      array([ 1.8527551 , 2.4294894 , 0.3062766],
      [array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
      array([ 1.8527551 , 2.4294894 , 0.3062766],
      [array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
      array([ 1.8527551 , 2.4294894 , 0.3062766]]


      Idea output:



      {'africa':array([-0.34219775,  0.61445   ,  0.19807251],
      'trip':array([-0.34219775, 0.61445 , 0.19807251, 0.15776388]}etc..


      I tried many ways to remove outer list:
      flatten-list-of-lists



      concatenate-item-in-list-to-strings



      how-to-convert-nested-list-into-dictionary-in-python-where-lst00-is-the-key
      but they don't work in this occasion, can anyone please help? Thank you in advanced !







      python dictionary






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 30 at 3:16









      Osca

      1709




      1709
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You seem to want both location and type to be independently mapped to the same values. You'd need to make the dict comprehension use nested loops to pull this off, since each value in YiW is a list of keys to make, not a single key. Simple approach:



          DY = {key: value for keys, value in zip(YiW, YiV) for key in keys}


          Note that this will drop data if any key appears more than once (so if YiW contains both ["africa", "trip"] and later ["asia", "trip"], you'll only map "trip" to the value that paired with ["asia", "trip"]). If that's not what you want, you'll need to be more specific in the desired behavior.






          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',
            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%2f52088762%2fmaking-two-lists-into-a-dictionary-error-unhashable-type-list%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








            up vote
            1
            down vote



            accepted










            You seem to want both location and type to be independently mapped to the same values. You'd need to make the dict comprehension use nested loops to pull this off, since each value in YiW is a list of keys to make, not a single key. Simple approach:



            DY = {key: value for keys, value in zip(YiW, YiV) for key in keys}


            Note that this will drop data if any key appears more than once (so if YiW contains both ["africa", "trip"] and later ["asia", "trip"], you'll only map "trip" to the value that paired with ["asia", "trip"]). If that's not what you want, you'll need to be more specific in the desired behavior.






            share|improve this answer



























              up vote
              1
              down vote



              accepted










              You seem to want both location and type to be independently mapped to the same values. You'd need to make the dict comprehension use nested loops to pull this off, since each value in YiW is a list of keys to make, not a single key. Simple approach:



              DY = {key: value for keys, value in zip(YiW, YiV) for key in keys}


              Note that this will drop data if any key appears more than once (so if YiW contains both ["africa", "trip"] and later ["asia", "trip"], you'll only map "trip" to the value that paired with ["asia", "trip"]). If that's not what you want, you'll need to be more specific in the desired behavior.






              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You seem to want both location and type to be independently mapped to the same values. You'd need to make the dict comprehension use nested loops to pull this off, since each value in YiW is a list of keys to make, not a single key. Simple approach:



                DY = {key: value for keys, value in zip(YiW, YiV) for key in keys}


                Note that this will drop data if any key appears more than once (so if YiW contains both ["africa", "trip"] and later ["asia", "trip"], you'll only map "trip" to the value that paired with ["asia", "trip"]). If that's not what you want, you'll need to be more specific in the desired behavior.






                share|improve this answer














                You seem to want both location and type to be independently mapped to the same values. You'd need to make the dict comprehension use nested loops to pull this off, since each value in YiW is a list of keys to make, not a single key. Simple approach:



                DY = {key: value for keys, value in zip(YiW, YiV) for key in keys}


                Note that this will drop data if any key appears more than once (so if YiW contains both ["africa", "trip"] and later ["asia", "trip"], you'll only map "trip" to the value that paired with ["asia", "trip"]). If that's not what you want, you'll need to be more specific in the desired behavior.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 at 18:09

























                answered Aug 30 at 3:24









                ShadowRanger

                56k44890




                56k44890






























                    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.





                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52088762%2fmaking-two-lists-into-a-dictionary-error-unhashable-type-list%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'