{i:1, j:2, k:3} return (1,2,3) How can you do this?
Asked
Active
Viewed 68 times
0
-
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 Answers
0
dictionary = {'A':True, 'B':False, 'C':True, 'D':False}
my_list = [key for key in dictionary.keys() if dictionary[key]]

Kraigolas
- 5,121
- 3
- 12
- 37
0
Try this:
dict_ = {'A':True, 'B':False, 'C':True, 'D':False}
keys = [key for key, value in dict_.items() if value]

sarartur
- 1,178
- 1
- 4
- 13
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,517
- 1
- 12
- 29
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.
- 40,517
- 4
- 31
- 51
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,638
- 4
- 19
- 26

dror_python_encoder
- 11
- 4