0

{i:1, j:2, k:3} return (1,2,3) How can you do this?

  • Please read [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) and the [Welcome to Stack Overflow](https://stackoverflow.com/tour) introduction tour. "Show me how to solve this coding problem" is [off-topic for Stack Overflow](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). We expect you to make an [honest attempt at the solution](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), and then ask a specific question about your implementation. – martineau Feb 19 '21 at 21:25
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Sabito 錆兎 stands with Ukraine Feb 27 '21 at 01:17

6 Answers6

0
 dictionary = {'A':True, 'B':False, 'C':True, 'D':False} 
 my_list = [key for key in dictionary.keys() if dictionary[key]]
Kraigolas
  • 3,728
  • 3
  • 8
  • 27
0

Try this:

dict_ = {'A':True, 'B':False, 'C':True, 'D':False} 
keys = [key for key, value in dict_.items() if value]
sarartur
  • 1,048
  • 1
  • 3
  • 12
0

By this list compression, you can get the specific keys which value is true

[i for i,j in a.items() if j]
Sohaib Anwaar
  • 1,317
  • 10
  • 24
0

You could use a list comprehension like this:

D = {'A':True, 'B':False, 'C':True, 'D':False}

L = [k for k in D if D[k]] # ['A', 'C']
Alain T.
  • 34,859
  • 4
  • 30
  • 47
0

Setting up the data

my_dictionary = {'A':True, 'B':False, 'C':True, 'D':False}

Now go through the dictionary and add it to the list if the dictionary key is True

my_list = [i for i in my_dictionary if my_dictionary[i]]

Which gives

['A', 'C']
Paul Brennan
  • 2,399
  • 3
  • 14
  • 20
0

Another way would be to use filter -


D = {'A':True, 'B':False, 'C':True, 'D':False}

>>> list(filter(lambda x: D[x],D))
['A', 'C']

Vaebhav
  • 3,661
  • 1
  • 11
  • 25