Search in a nested list of a dictionary
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ...
.
I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.
elements =
0: [((0, -4), 1)],
1: [((1, -5), 1), ((0, -4), 1)],
2: [((2, -3), 1)],
3: [((3, -2), 1), ((0, -5), 1)],
max_c = 0
for a in list(elements):
for (b, c), d in elements[a]:
max_c = max(max_c, abs(c))
Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?
Thanks in advance.
python dictionary
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ...
.
I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.
elements =
0: [((0, -4), 1)],
1: [((1, -5), 1), ((0, -4), 1)],
2: [((2, -3), 1)],
3: [((3, -2), 1), ((0, -5), 1)],
max_c = 0
for a in list(elements):
for (b, c), d in elements[a]:
max_c = max(max_c, abs(c))
Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?
Thanks in advance.
python dictionary
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ...
.
I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.
elements =
0: [((0, -4), 1)],
1: [((1, -5), 1), ((0, -4), 1)],
2: [((2, -3), 1)],
3: [((3, -2), 1), ((0, -5), 1)],
max_c = 0
for a in list(elements):
for (b, c), d in elements[a]:
max_c = max(max_c, abs(c))
Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?
Thanks in advance.
python dictionary
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ...
.
I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.
elements =
0: [((0, -4), 1)],
1: [((1, -5), 1), ((0, -4), 1)],
2: [((2, -3), 1)],
3: [((3, -2), 1), ((0, -5), 1)],
max_c = 0
for a in list(elements):
for (b, c), d in elements[a]:
max_c = max(max_c, abs(c))
Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?
Thanks in advance.
python dictionary
python dictionary
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 hours ago
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
Xbel
235
235
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
If you were to extract all abs(c)
into a list, you could use max
over that list.
Fortunately, this is easy using your loops and a list-comprehension:
Cs = [
abs(value)
for collection in elements.values()
for (_, value), _ in collection
]
max_c = max(Cs)
Note the use of elements.values()
instead of extracting the values manually (elements[a]
) and the use of _
as a throwaway variable.
Now max
works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:
max_c = max(
abs(value)
for collection in elements.values()
for (_, value), _ in collection
)
add a comment |Â
up vote
2
down vote
When you are looping over the keys
a
and valueselements[a]
of a dictionary, use theitems
method to loop over both at the same time:for a, value in elements.items():
for (b, c), d in value:(If I knew what these values represented, I would pick a better name than
value
.)The code here doesn't use the keys, it only uses the values. So use the
values
method instead:for value in elements.values():
for (b, c), d in value:The code doesn't use
b
ord
either. It is conventional to use the name_
for unused variables:for (_, c), _ in value:
A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using
itertools.chain.from_iterable
:from itertools import chain
for (_, c), _ in chain.from_iterable(elements.values()):The repeated calls to
max
can become a single call taking a generator expression:values = chain.from_iterable(elements.values())
max_abs_c = max(abs(c) for (_, c), _ in values)It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you were to extract all abs(c)
into a list, you could use max
over that list.
Fortunately, this is easy using your loops and a list-comprehension:
Cs = [
abs(value)
for collection in elements.values()
for (_, value), _ in collection
]
max_c = max(Cs)
Note the use of elements.values()
instead of extracting the values manually (elements[a]
) and the use of _
as a throwaway variable.
Now max
works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:
max_c = max(
abs(value)
for collection in elements.values()
for (_, value), _ in collection
)
add a comment |Â
up vote
2
down vote
If you were to extract all abs(c)
into a list, you could use max
over that list.
Fortunately, this is easy using your loops and a list-comprehension:
Cs = [
abs(value)
for collection in elements.values()
for (_, value), _ in collection
]
max_c = max(Cs)
Note the use of elements.values()
instead of extracting the values manually (elements[a]
) and the use of _
as a throwaway variable.
Now max
works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:
max_c = max(
abs(value)
for collection in elements.values()
for (_, value), _ in collection
)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you were to extract all abs(c)
into a list, you could use max
over that list.
Fortunately, this is easy using your loops and a list-comprehension:
Cs = [
abs(value)
for collection in elements.values()
for (_, value), _ in collection
]
max_c = max(Cs)
Note the use of elements.values()
instead of extracting the values manually (elements[a]
) and the use of _
as a throwaway variable.
Now max
works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:
max_c = max(
abs(value)
for collection in elements.values()
for (_, value), _ in collection
)
If you were to extract all abs(c)
into a list, you could use max
over that list.
Fortunately, this is easy using your loops and a list-comprehension:
Cs = [
abs(value)
for collection in elements.values()
for (_, value), _ in collection
]
max_c = max(Cs)
Note the use of elements.values()
instead of extracting the values manually (elements[a]
) and the use of _
as a throwaway variable.
Now max
works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:
max_c = max(
abs(value)
for collection in elements.values()
for (_, value), _ in collection
)
answered 30 mins ago


Mathias Ettinger
22.5k33077
22.5k33077
add a comment |Â
add a comment |Â
up vote
2
down vote
When you are looping over the keys
a
and valueselements[a]
of a dictionary, use theitems
method to loop over both at the same time:for a, value in elements.items():
for (b, c), d in value:(If I knew what these values represented, I would pick a better name than
value
.)The code here doesn't use the keys, it only uses the values. So use the
values
method instead:for value in elements.values():
for (b, c), d in value:The code doesn't use
b
ord
either. It is conventional to use the name_
for unused variables:for (_, c), _ in value:
A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using
itertools.chain.from_iterable
:from itertools import chain
for (_, c), _ in chain.from_iterable(elements.values()):The repeated calls to
max
can become a single call taking a generator expression:values = chain.from_iterable(elements.values())
max_abs_c = max(abs(c) for (_, c), _ in values)It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.
add a comment |Â
up vote
2
down vote
When you are looping over the keys
a
and valueselements[a]
of a dictionary, use theitems
method to loop over both at the same time:for a, value in elements.items():
for (b, c), d in value:(If I knew what these values represented, I would pick a better name than
value
.)The code here doesn't use the keys, it only uses the values. So use the
values
method instead:for value in elements.values():
for (b, c), d in value:The code doesn't use
b
ord
either. It is conventional to use the name_
for unused variables:for (_, c), _ in value:
A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using
itertools.chain.from_iterable
:from itertools import chain
for (_, c), _ in chain.from_iterable(elements.values()):The repeated calls to
max
can become a single call taking a generator expression:values = chain.from_iterable(elements.values())
max_abs_c = max(abs(c) for (_, c), _ in values)It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
When you are looping over the keys
a
and valueselements[a]
of a dictionary, use theitems
method to loop over both at the same time:for a, value in elements.items():
for (b, c), d in value:(If I knew what these values represented, I would pick a better name than
value
.)The code here doesn't use the keys, it only uses the values. So use the
values
method instead:for value in elements.values():
for (b, c), d in value:The code doesn't use
b
ord
either. It is conventional to use the name_
for unused variables:for (_, c), _ in value:
A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using
itertools.chain.from_iterable
:from itertools import chain
for (_, c), _ in chain.from_iterable(elements.values()):The repeated calls to
max
can become a single call taking a generator expression:values = chain.from_iterable(elements.values())
max_abs_c = max(abs(c) for (_, c), _ in values)It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.
When you are looping over the keys
a
and valueselements[a]
of a dictionary, use theitems
method to loop over both at the same time:for a, value in elements.items():
for (b, c), d in value:(If I knew what these values represented, I would pick a better name than
value
.)The code here doesn't use the keys, it only uses the values. So use the
values
method instead:for value in elements.values():
for (b, c), d in value:The code doesn't use
b
ord
either. It is conventional to use the name_
for unused variables:for (_, c), _ in value:
A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using
itertools.chain.from_iterable
:from itertools import chain
for (_, c), _ in chain.from_iterable(elements.values()):The repeated calls to
max
can become a single call taking a generator expression:values = chain.from_iterable(elements.values())
max_abs_c = max(abs(c) for (_, c), _ in values)It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.
edited 24 mins ago
answered 29 mins ago
Gareth Rees
43.5k396175
43.5k396175
add a comment |Â
add a comment |Â
Xbel is a new contributor. Be nice, and check out our Code of Conduct.
Xbel is a new contributor. Be nice, and check out our Code of Conduct.
Xbel is a new contributor. Be nice, and check out our Code of Conduct.
Xbel is a new contributor. Be nice, and check out our Code of Conduct.
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%2fcodereview.stackexchange.com%2fquestions%2f205733%2fsearch-in-a-nested-list-of-a-dictionary%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