-2

Here is a dictionary:

my_dict = {'id': '5f7756f5141a30516ba0fe1d', 
       'name': 'Flask', 
       'desc': '', 
       'descData': None, 
       'closed': False, 
       'idOrganization': None, 
       'idEnterprise': None, 
       'pinned': False,}

What I need:
A good code sample that uses a loop/filter/map (whatever it takes) to get this filtered result:

{'name': 'Flask', 'idOrganization': None}

My Problem:
I assumed I would loop through the keys with dummy code something like:

for the_key in my_dict:
    if the_key == 'name' or 'idOrginization':
       print (the key with its value)

I can get keys back from if statement, but I need the values along with it as well.

2 Answers2

1

{i: my_dict[i] for i in ['name', 'idOrganization']}

scp10011
  • 136
  • 3
-1

You can use items() function

for k, v in my_dict.items():
    print(k, ' = ', v)

You can then use in for your conditional check to avoid several or as below:

for k, v in my_dict.items():
    if k in ('name', 'idOrginization',):
        print(k, ' = ', v)
Wafula Samuel
  • 530
  • 1
  • 8
  • 25