Home > Enterprise >  How to compare dates for a nested dictionary values
How to compare dates for a nested dictionary values

Time:07-22

Data={
    0:{
       'country1': "Ryan lives in America",
       'country_one': "5",
       'med_dict':{
          0: {
              'endDate': "732",
              'endTime': "89",
             }
       },
     },
    1: {
        'country1': "Sam lives in america",
        'country_three': "90",
        'med_dict': {
          0: {
              'endDate': "245",
              'endTime': "222",
              }
        },

    }
}

res = [v1['country1'] for k1, v1 in Data.items()]
print(res)

I have got the output as ['Ryan lives in America', 'Sam lives in america']. I want to print True if word 'America/america' is present in both the string or else print False. Need to check for the case sensitivity as lower or upper. How to do it?

CodePudding user response:

If maybe you want to check multiple words, you can create list of words and use any.

chk = ['America', 'americ']

res = [any(c in v1['country1'] for c in chk) for k1,v1 in Data.items()]
print(res)

# If you want to check and print
if all(res):
    print('True')
else:
    print('False')

[True, True]

CodePudding user response:

Use lower to do the case insensitive comparison, and then all on boolean values:

res = all("america" in v1['country1'].lower() for k1, v1 in Data.items())
print(res)

Output

True
  • Related