Python: can we get the value from the dict or object like the ts?









up vote
2
down vote

favorite
1












Isn't the python can get the object or dict like ts:






let test = a: 1, b: 2, c:3
const a, b = test




The a and b are the key and the variable, it looks simple and not need two line code or for...in to get the a and b.








share|improve this question

























    up vote
    2
    down vote

    favorite
    1












    Isn't the python can get the object or dict like ts:






    let test = a: 1, b: 2, c:3
    const a, b = test




    The a and b are the key and the variable, it looks simple and not need two line code or for...in to get the a and b.








    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      Isn't the python can get the object or dict like ts:






      let test = a: 1, b: 2, c:3
      const a, b = test




      The a and b are the key and the variable, it looks simple and not need two line code or for...in to get the a and b.








      share|improve this question













      Isn't the python can get the object or dict like ts:






      let test = a: 1, b: 2, c:3
      const a, b = test




      The a and b are the key and the variable, it looks simple and not need two line code or for...in to get the a and b.




      let test = a: 1, b: 2, c:3
      const a, b = test





      let test = a: 1, b: 2, c:3
      const a, b = test






      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 13:36









      yesterday_time

      135




      135






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can do something like this.



          >>>
          >>> d = "a": 1, "b": 2, "c": 3
          >>>
          >>> a, b, c = (lambda a, b, c: (a, b, c))(**d)
          >>> a
          1
          >>> b
          2
          >>> c
          3
          >>>


          Here is another example where we will only get few values.



          >>> d2 = "a": 1, "b": 2, "c": 3, "d": 4
          >>> a, b = (lambda a, b, **kwargs: (a, b))(**d2)
          >>> a
          1
          >>> b
          2
          >>>





          share|improve this answer






















          • We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
            – yesterday_time
            Nov 10 at 13:57










          • In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
            – hygull
            Nov 10 at 14:01


















          up vote
          1
          down vote













          The closest you'll get is something along the lines of:



          a, b = test['a'], test['b'] # dicts
          a, b = test.a, test.b # objects


          Or, less repetitive if you have more:



          a, b, c = (test[i] for i in ('a', 'b', 'c')) # dicts
          a, b, c = (getattr(test, i) for i in ('a', 'b', 'c')) # objects


          You could probably do funky things with using the locals() or globals() dicts and .updateing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.






          share|improve this answer






















          • Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
            – Torxed
            Nov 10 at 13:43







          • 1




            You can't always predict the order of dict values, so… nah.
            – deceze
            Nov 10 at 13:44










          • OrderedDict if you're worried about that.
            – Torxed
            Nov 10 at 13:44










          • That's already so much declarative overhead that you may as well use one of these proposals here.
            – deceze
            Nov 10 at 13:45






          • 1




            @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
            – deceze
            Nov 10 at 13:57

















          up vote
          1
          down vote













          Just use two lines. Anything else which works in one line is too clever, too hard to understand, and too easy to break in the future:



          test = 'a': 1, 'b': 2, 'c':3
          a = test['a']
          b = test['b']





          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%2f53239513%2fpython-can-we-get-the-value-from-the-dict-or-object-like-the-ts%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You can do something like this.



            >>>
            >>> d = "a": 1, "b": 2, "c": 3
            >>>
            >>> a, b, c = (lambda a, b, c: (a, b, c))(**d)
            >>> a
            1
            >>> b
            2
            >>> c
            3
            >>>


            Here is another example where we will only get few values.



            >>> d2 = "a": 1, "b": 2, "c": 3, "d": 4
            >>> a, b = (lambda a, b, **kwargs: (a, b))(**d2)
            >>> a
            1
            >>> b
            2
            >>>





            share|improve this answer






















            • We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
              – yesterday_time
              Nov 10 at 13:57










            • In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
              – hygull
              Nov 10 at 14:01















            up vote
            1
            down vote



            accepted










            You can do something like this.



            >>>
            >>> d = "a": 1, "b": 2, "c": 3
            >>>
            >>> a, b, c = (lambda a, b, c: (a, b, c))(**d)
            >>> a
            1
            >>> b
            2
            >>> c
            3
            >>>


            Here is another example where we will only get few values.



            >>> d2 = "a": 1, "b": 2, "c": 3, "d": 4
            >>> a, b = (lambda a, b, **kwargs: (a, b))(**d2)
            >>> a
            1
            >>> b
            2
            >>>





            share|improve this answer






















            • We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
              – yesterday_time
              Nov 10 at 13:57










            • In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
              – hygull
              Nov 10 at 14:01













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You can do something like this.



            >>>
            >>> d = "a": 1, "b": 2, "c": 3
            >>>
            >>> a, b, c = (lambda a, b, c: (a, b, c))(**d)
            >>> a
            1
            >>> b
            2
            >>> c
            3
            >>>


            Here is another example where we will only get few values.



            >>> d2 = "a": 1, "b": 2, "c": 3, "d": 4
            >>> a, b = (lambda a, b, **kwargs: (a, b))(**d2)
            >>> a
            1
            >>> b
            2
            >>>





            share|improve this answer














            You can do something like this.



            >>>
            >>> d = "a": 1, "b": 2, "c": 3
            >>>
            >>> a, b, c = (lambda a, b, c: (a, b, c))(**d)
            >>> a
            1
            >>> b
            2
            >>> c
            3
            >>>


            Here is another example where we will only get few values.



            >>> d2 = "a": 1, "b": 2, "c": 3, "d": 4
            >>> a, b = (lambda a, b, **kwargs: (a, b))(**d2)
            >>> a
            1
            >>> b
            2
            >>>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 14:03

























            answered Nov 10 at 13:43









            hygull

            2,70311126




            2,70311126











            • We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
              – yesterday_time
              Nov 10 at 13:57










            • In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
              – hygull
              Nov 10 at 14:01

















            • We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
              – yesterday_time
              Nov 10 at 13:57










            • In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
              – hygull
              Nov 10 at 14:01
















            We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
            – yesterday_time
            Nov 10 at 13:57




            We need enum all keys in the lambda. if I have many keys and want to get some values by the keys, it's not a good idea.
            – yesterday_time
            Nov 10 at 13:57












            In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
            – hygull
            Nov 10 at 14:01





            In that case, what if you will use this statement, a, b = (lambda a, b, **kwargs: (a, b))(**d).
            – hygull
            Nov 10 at 14:01













            up vote
            1
            down vote













            The closest you'll get is something along the lines of:



            a, b = test['a'], test['b'] # dicts
            a, b = test.a, test.b # objects


            Or, less repetitive if you have more:



            a, b, c = (test[i] for i in ('a', 'b', 'c')) # dicts
            a, b, c = (getattr(test, i) for i in ('a', 'b', 'c')) # objects


            You could probably do funky things with using the locals() or globals() dicts and .updateing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.






            share|improve this answer






















            • Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
              – Torxed
              Nov 10 at 13:43







            • 1




              You can't always predict the order of dict values, so… nah.
              – deceze
              Nov 10 at 13:44










            • OrderedDict if you're worried about that.
              – Torxed
              Nov 10 at 13:44










            • That's already so much declarative overhead that you may as well use one of these proposals here.
              – deceze
              Nov 10 at 13:45






            • 1




              @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
              – deceze
              Nov 10 at 13:57














            up vote
            1
            down vote













            The closest you'll get is something along the lines of:



            a, b = test['a'], test['b'] # dicts
            a, b = test.a, test.b # objects


            Or, less repetitive if you have more:



            a, b, c = (test[i] for i in ('a', 'b', 'c')) # dicts
            a, b, c = (getattr(test, i) for i in ('a', 'b', 'c')) # objects


            You could probably do funky things with using the locals() or globals() dicts and .updateing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.






            share|improve this answer






















            • Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
              – Torxed
              Nov 10 at 13:43







            • 1




              You can't always predict the order of dict values, so… nah.
              – deceze
              Nov 10 at 13:44










            • OrderedDict if you're worried about that.
              – Torxed
              Nov 10 at 13:44










            • That's already so much declarative overhead that you may as well use one of these proposals here.
              – deceze
              Nov 10 at 13:45






            • 1




              @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
              – deceze
              Nov 10 at 13:57












            up vote
            1
            down vote










            up vote
            1
            down vote









            The closest you'll get is something along the lines of:



            a, b = test['a'], test['b'] # dicts
            a, b = test.a, test.b # objects


            Or, less repetitive if you have more:



            a, b, c = (test[i] for i in ('a', 'b', 'c')) # dicts
            a, b, c = (getattr(test, i) for i in ('a', 'b', 'c')) # objects


            You could probably do funky things with using the locals() or globals() dicts and .updateing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.






            share|improve this answer














            The closest you'll get is something along the lines of:



            a, b = test['a'], test['b'] # dicts
            a, b = test.a, test.b # objects


            Or, less repetitive if you have more:



            a, b, c = (test[i] for i in ('a', 'b', 'c')) # dicts
            a, b, c = (getattr(test, i) for i in ('a', 'b', 'c')) # objects


            You could probably do funky things with using the locals() or globals() dicts and .updateing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 13:45

























            answered Nov 10 at 13:39









            deceze

            387k60523675




            387k60523675











            • Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
              – Torxed
              Nov 10 at 13:43







            • 1




              You can't always predict the order of dict values, so… nah.
              – deceze
              Nov 10 at 13:44










            • OrderedDict if you're worried about that.
              – Torxed
              Nov 10 at 13:44










            • That's already so much declarative overhead that you may as well use one of these proposals here.
              – deceze
              Nov 10 at 13:45






            • 1




              @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
              – deceze
              Nov 10 at 13:57
















            • Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
              – Torxed
              Nov 10 at 13:43







            • 1




              You can't always predict the order of dict values, so… nah.
              – deceze
              Nov 10 at 13:44










            • OrderedDict if you're worried about that.
              – Torxed
              Nov 10 at 13:44










            • That's already so much declarative overhead that you may as well use one of these proposals here.
              – deceze
              Nov 10 at 13:45






            • 1




              @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
              – deceze
              Nov 10 at 13:57















            Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
            – Torxed
            Nov 10 at 13:43





            Actually, the closest you'll get is a, b = test.values(), assuming you know the number of elements returned.
            – Torxed
            Nov 10 at 13:43





            1




            1




            You can't always predict the order of dict values, so… nah.
            – deceze
            Nov 10 at 13:44




            You can't always predict the order of dict values, so… nah.
            – deceze
            Nov 10 at 13:44












            OrderedDict if you're worried about that.
            – Torxed
            Nov 10 at 13:44




            OrderedDict if you're worried about that.
            – Torxed
            Nov 10 at 13:44












            That's already so much declarative overhead that you may as well use one of these proposals here.
            – deceze
            Nov 10 at 13:45




            That's already so much declarative overhead that you may as well use one of these proposals here.
            – deceze
            Nov 10 at 13:45




            1




            1




            @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
            – deceze
            Nov 10 at 13:57




            @NCh Even if you can rely on the dict order staying the same, can you rely on the order the dict has been constructed in?! In the Javascript example, the order of the object doesn't matter. There's no equivalent in Python that makes the same guarantees.
            – deceze
            Nov 10 at 13:57










            up vote
            1
            down vote













            Just use two lines. Anything else which works in one line is too clever, too hard to understand, and too easy to break in the future:



            test = 'a': 1, 'b': 2, 'c':3
            a = test['a']
            b = test['b']





            share|improve this answer
























              up vote
              1
              down vote













              Just use two lines. Anything else which works in one line is too clever, too hard to understand, and too easy to break in the future:



              test = 'a': 1, 'b': 2, 'c':3
              a = test['a']
              b = test['b']





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                Just use two lines. Anything else which works in one line is too clever, too hard to understand, and too easy to break in the future:



                test = 'a': 1, 'b': 2, 'c':3
                a = test['a']
                b = test['b']





                share|improve this answer












                Just use two lines. Anything else which works in one line is too clever, too hard to understand, and too easy to break in the future:



                test = 'a': 1, 'b': 2, 'c':3
                a = test['a']
                b = test['b']






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 14:21









                Ned Batchelder

                251k50438564




                251k50438564



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239513%2fpython-can-we-get-the-value-from-the-dict-or-object-like-the-ts%23new-answer', 'question_page');

                    );

                    Post as a guest














































































                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党