How to change the first element in a tuple that is a key in a dictionary?












0















I need to set the first element of a tuple which is a key in a dictionary to be current.



{('a', '0'): 'c',
('b', '0'): 'd',}


I need to set 'a' as current variable before looping the code.
How can I do this?










share|improve this question




















  • 3





    Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?

    – Austin Hastings
    Nov 22 '18 at 16:09
















0















I need to set the first element of a tuple which is a key in a dictionary to be current.



{('a', '0'): 'c',
('b', '0'): 'd',}


I need to set 'a' as current variable before looping the code.
How can I do this?










share|improve this question




















  • 3





    Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?

    – Austin Hastings
    Nov 22 '18 at 16:09














0












0








0








I need to set the first element of a tuple which is a key in a dictionary to be current.



{('a', '0'): 'c',
('b', '0'): 'd',}


I need to set 'a' as current variable before looping the code.
How can I do this?










share|improve this question
















I need to set the first element of a tuple which is a key in a dictionary to be current.



{('a', '0'): 'c',
('b', '0'): 'd',}


I need to set 'a' as current variable before looping the code.
How can I do this?







python dictionary tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 18:23









Eugene Yarmash

83.7k22177260




83.7k22177260










asked Nov 22 '18 at 16:02









pollywogzpollywogz

175




175








  • 3





    Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?

    – Austin Hastings
    Nov 22 '18 at 16:09














  • 3





    Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?

    – Austin Hastings
    Nov 22 '18 at 16:09








3




3





Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?

– Austin Hastings
Nov 22 '18 at 16:09





Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?

– Austin Hastings
Nov 22 '18 at 16:09












3 Answers
3






active

oldest

votes


















1














You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:



key1, key2 = ('a', '0'), (current, 0)

d[key2] = d[key1]
del d[key1]


or in one step:



d[key2] = d.pop(key1)





share|improve this answer

































    0














    If you want to loop through all your dictionaries to update them only by the first item in your tuple key.



    d = {('a', '0'): 'c', ('b', '0'): 'd',}

    for key in d:
    new_key = key[0]
    d[new_key] = d.pop(key)

    d
    >>{'a': 'c', 'b': 'd'}


    Essentially here you are deleting the old key and replacing it with a new one where you take index 0 of your tuple to be the new key.






    share|improve this answer































      0














      Are you trying to access 'a' from the dictionary?
      If you are then you can do the following:



      list(d.keys())[0][0] 
      >>> a


      However; if the above is not what is required from you but the below is.



      {('a', '0'): 'a', ('b', '0'): 'd'}


      You can accomplish this with lambda or with list comprehension:



      # Lambda
      d[('a', '0')] = list(map(lambda k: k[0], d))[0]
      print(d)
      >>> {('a', '0'): 'a', ('b', '0'): 'd'}

      # list comprehension
      d[('a', '0')] = [k[0] for k in d][0]
      >>> {('a', '0'): 'a', ('b', '0'): 'd'}


      Or are you looking to achieve something as this:



      {('b', '0'): 'd', ('c', '0'): 'a'}


      Again, with lambda or with list comprehension:



      # Lambda
      d[('a', '0')] = list(map(lambda k: k[0], d))[0]
      d[('c', '0')] = d.pop(('a', '0'))
      print(d)
      >>> {('b', '0'): 'd', ('c', '0'): 'a'}

      # list comprehension
      d[('a', '0')] = [k[0] for k in d][0]
      d[('c', '0')] = d.pop(('a', '0'))
      print(d)
      >>> {('b', '0'): 'd', ('c', '0'): 'a'}





      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%2f53434651%2fhow-to-change-the-first-element-in-a-tuple-that-is-a-key-in-a-dictionary%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 can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:



        key1, key2 = ('a', '0'), (current, 0)

        d[key2] = d[key1]
        del d[key1]


        or in one step:



        d[key2] = d.pop(key1)





        share|improve this answer






























          1














          You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:



          key1, key2 = ('a', '0'), (current, 0)

          d[key2] = d[key1]
          del d[key1]


          or in one step:



          d[key2] = d.pop(key1)





          share|improve this answer




























            1












            1








            1







            You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:



            key1, key2 = ('a', '0'), (current, 0)

            d[key2] = d[key1]
            del d[key1]


            or in one step:



            d[key2] = d.pop(key1)





            share|improve this answer















            You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:



            key1, key2 = ('a', '0'), (current, 0)

            d[key2] = d[key1]
            del d[key1]


            or in one step:



            d[key2] = d.pop(key1)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 12 '18 at 13:41

























            answered Nov 22 '18 at 16:05









            Eugene YarmashEugene Yarmash

            83.7k22177260




            83.7k22177260

























                0














                If you want to loop through all your dictionaries to update them only by the first item in your tuple key.



                d = {('a', '0'): 'c', ('b', '0'): 'd',}

                for key in d:
                new_key = key[0]
                d[new_key] = d.pop(key)

                d
                >>{'a': 'c', 'b': 'd'}


                Essentially here you are deleting the old key and replacing it with a new one where you take index 0 of your tuple to be the new key.






                share|improve this answer




























                  0














                  If you want to loop through all your dictionaries to update them only by the first item in your tuple key.



                  d = {('a', '0'): 'c', ('b', '0'): 'd',}

                  for key in d:
                  new_key = key[0]
                  d[new_key] = d.pop(key)

                  d
                  >>{'a': 'c', 'b': 'd'}


                  Essentially here you are deleting the old key and replacing it with a new one where you take index 0 of your tuple to be the new key.






                  share|improve this answer


























                    0












                    0








                    0







                    If you want to loop through all your dictionaries to update them only by the first item in your tuple key.



                    d = {('a', '0'): 'c', ('b', '0'): 'd',}

                    for key in d:
                    new_key = key[0]
                    d[new_key] = d.pop(key)

                    d
                    >>{'a': 'c', 'b': 'd'}


                    Essentially here you are deleting the old key and replacing it with a new one where you take index 0 of your tuple to be the new key.






                    share|improve this answer













                    If you want to loop through all your dictionaries to update them only by the first item in your tuple key.



                    d = {('a', '0'): 'c', ('b', '0'): 'd',}

                    for key in d:
                    new_key = key[0]
                    d[new_key] = d.pop(key)

                    d
                    >>{'a': 'c', 'b': 'd'}


                    Essentially here you are deleting the old key and replacing it with a new one where you take index 0 of your tuple to be the new key.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 16:07









                    BernardLBernardL

                    2,3581929




                    2,3581929























                        0














                        Are you trying to access 'a' from the dictionary?
                        If you are then you can do the following:



                        list(d.keys())[0][0] 
                        >>> a


                        However; if the above is not what is required from you but the below is.



                        {('a', '0'): 'a', ('b', '0'): 'd'}


                        You can accomplish this with lambda or with list comprehension:



                        # Lambda
                        d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                        print(d)
                        >>> {('a', '0'): 'a', ('b', '0'): 'd'}

                        # list comprehension
                        d[('a', '0')] = [k[0] for k in d][0]
                        >>> {('a', '0'): 'a', ('b', '0'): 'd'}


                        Or are you looking to achieve something as this:



                        {('b', '0'): 'd', ('c', '0'): 'a'}


                        Again, with lambda or with list comprehension:



                        # Lambda
                        d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                        d[('c', '0')] = d.pop(('a', '0'))
                        print(d)
                        >>> {('b', '0'): 'd', ('c', '0'): 'a'}

                        # list comprehension
                        d[('a', '0')] = [k[0] for k in d][0]
                        d[('c', '0')] = d.pop(('a', '0'))
                        print(d)
                        >>> {('b', '0'): 'd', ('c', '0'): 'a'}





                        share|improve this answer






























                          0














                          Are you trying to access 'a' from the dictionary?
                          If you are then you can do the following:



                          list(d.keys())[0][0] 
                          >>> a


                          However; if the above is not what is required from you but the below is.



                          {('a', '0'): 'a', ('b', '0'): 'd'}


                          You can accomplish this with lambda or with list comprehension:



                          # Lambda
                          d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                          print(d)
                          >>> {('a', '0'): 'a', ('b', '0'): 'd'}

                          # list comprehension
                          d[('a', '0')] = [k[0] for k in d][0]
                          >>> {('a', '0'): 'a', ('b', '0'): 'd'}


                          Or are you looking to achieve something as this:



                          {('b', '0'): 'd', ('c', '0'): 'a'}


                          Again, with lambda or with list comprehension:



                          # Lambda
                          d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                          d[('c', '0')] = d.pop(('a', '0'))
                          print(d)
                          >>> {('b', '0'): 'd', ('c', '0'): 'a'}

                          # list comprehension
                          d[('a', '0')] = [k[0] for k in d][0]
                          d[('c', '0')] = d.pop(('a', '0'))
                          print(d)
                          >>> {('b', '0'): 'd', ('c', '0'): 'a'}





                          share|improve this answer




























                            0












                            0








                            0







                            Are you trying to access 'a' from the dictionary?
                            If you are then you can do the following:



                            list(d.keys())[0][0] 
                            >>> a


                            However; if the above is not what is required from you but the below is.



                            {('a', '0'): 'a', ('b', '0'): 'd'}


                            You can accomplish this with lambda or with list comprehension:



                            # Lambda
                            d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                            print(d)
                            >>> {('a', '0'): 'a', ('b', '0'): 'd'}

                            # list comprehension
                            d[('a', '0')] = [k[0] for k in d][0]
                            >>> {('a', '0'): 'a', ('b', '0'): 'd'}


                            Or are you looking to achieve something as this:



                            {('b', '0'): 'd', ('c', '0'): 'a'}


                            Again, with lambda or with list comprehension:



                            # Lambda
                            d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                            d[('c', '0')] = d.pop(('a', '0'))
                            print(d)
                            >>> {('b', '0'): 'd', ('c', '0'): 'a'}

                            # list comprehension
                            d[('a', '0')] = [k[0] for k in d][0]
                            d[('c', '0')] = d.pop(('a', '0'))
                            print(d)
                            >>> {('b', '0'): 'd', ('c', '0'): 'a'}





                            share|improve this answer















                            Are you trying to access 'a' from the dictionary?
                            If you are then you can do the following:



                            list(d.keys())[0][0] 
                            >>> a


                            However; if the above is not what is required from you but the below is.



                            {('a', '0'): 'a', ('b', '0'): 'd'}


                            You can accomplish this with lambda or with list comprehension:



                            # Lambda
                            d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                            print(d)
                            >>> {('a', '0'): 'a', ('b', '0'): 'd'}

                            # list comprehension
                            d[('a', '0')] = [k[0] for k in d][0]
                            >>> {('a', '0'): 'a', ('b', '0'): 'd'}


                            Or are you looking to achieve something as this:



                            {('b', '0'): 'd', ('c', '0'): 'a'}


                            Again, with lambda or with list comprehension:



                            # Lambda
                            d[('a', '0')] = list(map(lambda k: k[0], d))[0]
                            d[('c', '0')] = d.pop(('a', '0'))
                            print(d)
                            >>> {('b', '0'): 'd', ('c', '0'): 'a'}

                            # list comprehension
                            d[('a', '0')] = [k[0] for k in d][0]
                            d[('c', '0')] = d.pop(('a', '0'))
                            print(d)
                            >>> {('b', '0'): 'd', ('c', '0'): 'a'}






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 23 '18 at 10:44

























                            answered Nov 22 '18 at 19:13









                            Victor SVictor S

                            1668




                            1668






























                                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%2f53434651%2fhow-to-change-the-first-element-in-a-tuple-that-is-a-key-in-a-dictionary%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'