mardi 1 juin 2021

Iterate a list in a dictionary, if the list exists in the dictionary. In Python

I have a dictionary in the following format: (In Python)

some_dict = {
 'some_list': ['val1', 'val2', 'val3']
}

The key 'some_list', doesn't always exist, but when it does, I want to loop its values. To do this I'm currently doing the following:

if 'some_list' in some_dict:
 for value in some_dict['some_list']
  #Run some code using value

Is this the best / most simple way to achive this goal, or is there a way to check for existence and loop in one line?

Aucun commentaire:

Enregistrer un commentaire