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 !
python dictionary
add a comment |
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 !
python dictionary
add a comment |
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 !
python dictionary
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
python dictionary
asked Aug 30 at 3:16
Osca
1709
1709
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 19 at 18:09
answered Aug 30 at 3:24
ShadowRanger
56k44890
56k44890
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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