-2

I have this

dict_1 = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)}}

And i would like this :

{"Apple","Orange","Banana","Lemon"}

What would be the correct comprehension here.

My attempts failed because of

TypeError: unhashable type: 'set'

means I can not unpack the set.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Easytoday
  • 1
  • 4

1 Answers1

0

If you only have a single key names 1 in the dict you can simply use

dict = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)}}
{key for key, val in dict[1]} # used the 1 assuming its the only key

Output:

{"Apple","Orange","Banana","Lemon"}

Edit:

Code with any number of variables(keys)

dict = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)},2:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)}}
resultArray = [{x for x, y in dict[eachkey]} for eachkey in {x for x, y in dict.items()} ]
print(resultArray)

Output:

[{'Orange', 'Apple', 'Lemon', 'Banana'}, {'Orange', 'Apple', 'Lemon', 'Banana'}]

Edit 2:

Code :

from itertools import chain
dict = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)},2:{("Juice",1),("Cocktail",1),("Milk",1),("Soup",1)}}
resultArray = [{x for x, y in dict[eachkey]} for eachkey in {x for x, y in dict.items()} ]
print(set().union(*(resultArray)))

Output:

{'Lemon', 'Soup', 'Milk', 'Cocktail', 'Banana', 'Apple', 'Orange', 'Juice'}
Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43
  • Thank you and you guess my next question, means with more key with the same structure.how to deal with multiple keys? – Easytoday Dec 15 '20 at 15:02
  • How to deal with dict_2 = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)}, \ 2:{("Juice",1),("Cocktail",1),("Milk",1),("Soup",1)}, \ 3: set(), \ 4:{("Breakfast",1),("Lunch",1),("Dinner",1)} } – Easytoday Dec 15 '20 at 15:08
  • @Easytoday checkout the edit i made to the answer for what you have asked above. Make sure to accept the answer if solved. – Sundeep Pidugu Dec 15 '20 at 15:22
  • Thank you again, your result is a list of set, is there a mean to just get a set, not a list? I try to replace brackets for curly bracket in your comprrehension and again i got the same error TypeError: unhashable type: 'set'. – Easytoday Dec 15 '20 at 15:33
  • whats ur expected result?, a single set of everything? – Sundeep Pidugu Dec 15 '20 at 15:35
  • I just expected a set of value, not a list. – Easytoday Dec 15 '20 at 15:36
  • you mean {'Banana', 'Lemon', 'Apple', 'Orange','Banana', 'Lemon', 'Apple', 'Orange'} like this? – Sundeep Pidugu Dec 15 '20 at 15:37
  • Please post the expected answer so that i can help you on that – Sundeep Pidugu Dec 15 '20 at 15:38
  • {'Orange', 'Apple', 'Lemon', 'Banana','Lunch', 'Breakfast', 'Dinner', 'Whatever'} – Easytoday Dec 15 '20 at 15:38
  • dict_2 = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)}, \ 2:{("Juice",1),("Cocktail",1),("Milk",1),("Soup",1)}, \ 3: set(), \ 4:{("Breakfast",1),("Lunch",1),("Dinner",1)} } – Easytoday Dec 15 '20 at 15:39
  • @Easytoday checkout the latest. Make sure to accept and mark the answer as solved if answered. – Sundeep Pidugu Dec 15 '20 at 15:49