Python: can we get the value from the dict or object like the ts?
up vote
2
down vote
favorite
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
.python
add a comment |
up vote
2
down vote
favorite
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
.python
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
.python
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
python
asked Nov 10 at 13:36
yesterday_time
135
135
add a comment |
add a comment |
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
>>>
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
add a comment |
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 .update
ing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.
Actually, the closest you'll get isa, 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
|
show 4 more comments
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']
add a comment |
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
>>>
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
add a comment |
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
>>>
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
add a comment |
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
>>>
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
>>>
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
add a comment |
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
add a comment |
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 .update
ing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.
Actually, the closest you'll get isa, 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
|
show 4 more comments
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 .update
ing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.
Actually, the closest you'll get isa, 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
|
show 4 more comments
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 .update
ing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.
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 .update
ing them with a set intersection for example, but you should always explicitly declare variables and not muck around with scope dicts.
edited Nov 10 at 13:45
answered Nov 10 at 13:39
deceze♦
387k60523675
387k60523675
Actually, the closest you'll get isa, 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
|
show 4 more comments
Actually, the closest you'll get isa, 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
|
show 4 more comments
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']
add a comment |
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']
add a comment |
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']
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']
answered Nov 10 at 14:21
Ned Batchelder
251k50438564
251k50438564
add a comment |
add a comment |
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
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
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
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
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