0

Here is my list with multiple dictionaries inside:

tags: [{
        'key': 'Environment',
        'value': 'Production'
    }, {
        'key': 'Environment',
        'value': 'Acceptance'
    }, {
        'key': 'Environment',
        'value': 'Test'
    }, {
        'key': 'Environment',
        'value': 'Development'
    }, {
        'key': 'Environment',
        'value': 'Sandbox'
    }]

Here the key value is always 'Environment' for all dictionaries. I need to check if the key is Environment, check if values are Production,Test,Development and Sandbox. If yes, print something.

How can achieve this ?

Ebin Davis
  • 5,421
  • 3
  • 15
  • 20
  • 1
    Please show what you've tried so far. It's customary on SO, as someone with a couple of hundred reps should know. – ShlomiF Nov 28 '18 at 10:39
  • The marked duplicate has many examples. If you need something more specific and are stuck, [edit your question](https://stackoverflow.com/posts/53517423/edit) with code from your latest attempts. – jpp Nov 28 '18 at 10:41

1 Answers1

1

You could use all:

tags = [{
        'key': 'Environment',
        'value': 'Production'
    }, {
        'key': 'Environment',
        'value': 'Acceptance'
    }, {
        'key': 'Environment',
        'value': 'Test'
    }, {
        'key': 'Environment',
        'value': 'Development'
    }, {
        'key': 'Environment',
        'value': 'Sandbox'
    }]

result = all(tag['value'] in ('Sandbox', 'Test', 'Development', 'Production') for tag in tags if tag['key'] == 'Environment')
print(result)

Output

False

Note that the output is False because it contains the value 'Acceptance'.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76