Neurobion
Neurobion

Reputation: 25

Find the value of the defined key of a dict within a list, by matching the dict values

Input is simple csv file with header what looks like:

dc,environment,type,component,fqdn,distribution,release
1,prod,web,fo,aa,ubuntu,14.04.5

It is loaded by csv.DictReader(csv) into the server_list:

def get_server(**kwargs):
    f = open("servers.dat", 'rt')
    try:
        reader = csv.DictReader(f)
        server_list = []
        for row in reader:
            server_list.append(row)
    finally:
        f.close()

List contains:

{'component': 'a', 'dc': '1', 'fqdn': 'aa', 'environment': 'prod', 'release': '14.04.5', 'distribution': 'ubuntu', 'type': 'web'}
{'component': 'a', 'dc': '1', 'fqdn': 'bb', 'environment': 'prod', 'release': '14.04.5', 'distribution': 'ubuntu', 'type': 'web'}
{'component': 'b', 'dc': '1', 'fqdn': 'cc', 'environment': 'prod', 'release': '12.04.5', 'distribution': 'ubuntu', 'type': 'web'}
{'component': 'a', 'dc': '1', 'fqdn': 'dd', 'environment': 'test02', 'release': '14.04.5', 'distribution': 'ubuntu', 'type': 'web'}

I would like to get value of fqdn when input will be e.g. dc = 1 and component = a from python function called e.g. get_foo(dc='1', component='a', environment=None) and defined def get_foo(**kwargs) or differently. Only result is required.

So, expected result in this case are all these lines except 3rd.

Thank you

Upvotes: 2

Views: 71

Answers (2)

wildwilhelm
wildwilhelm

Reputation: 5019

Your question states that you have a list of dicts, something like this:

>>> a = [{'component': 'a',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'prod',
          'fqdn': 'aa',
          'release': '14.04.5',
          'type': 'web'},
         {'component': 'a',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'prod',
          'fqdn': 'bb',
          'release': '14.04.5',
          'type': 'web'},
         {'component': 'b',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'prod',
          'fqdn': 'cc',
          'release': '12.04.5',
          'type': 'web'},
         {'component': 'a',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'test02',
          'fqdn': 'dd',
          'release': '14.04.5',
          'type': 'web'}]

You can always just filter the list of dicts using list comprehension syntax:

>>> [x['fqdn'] for x in a if x['dc'] == '1' and x['component'] == 'a']
['aa', 'bb', 'dd']

To wrap it up in a function:

def get_foo(dlist, dc='1', component='a'):
    return [dv['fqdn'] for dv in dlist
            if dv['dc'] == dc
            and dv['component'] == component]

And then:

>>> get_foo(a)
['aa', 'bb', 'dd']

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 60944

More generally

def get_foo(l, **kwargs):
    return [x['fqdn'] for x in l if all(x[i] == kwargs[i] for i in kwargs)]

This will throw a KeyError exception is you pass a keyword argument not in the dictionary x

Upvotes: 4

Related Questions