Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to check if keys exists and retrieve value from Dictionary in descending priority

I have a dictionary and I would like to get some values from it based on some keys. For example, I have a dictionary for users with their first name, last name, username, address, age and so on. Let's say, I only want to get one value (name) - either last name or first name or username but in descending priority like shown below:

(1) last name: if key exists, get value and stop checking. If not, move to next key.

(2) first name: if key exists, get value and stop checking. If not, move to next key.

(3) username: if key exists, get value or return null/empty

#my dict looks something like this myDict = {'age': ['value'], 'address': ['value1, value2'],           'firstName': ['value'], 'lastName': ['']}  #List of keys I want to check in descending priority: lastName > firstName > userName keySet = ['lastName', 'firstName', 'userName'] 

What I tried doing is to get all the possible values and put them into a list so I can retrieve the first element in the list. Obviously it didn't work out.

tempList = []  for key in keys:     get_value = myDict.get(key)     tempList .append(get_value) 

Is there a better way to do this without using if else block?

like image 971
Cryssie Avatar asked Jun 25 '13 22:06

Cryssie


People also ask

How do you check if a key-value pair exists in a dictionary Python?

To check if a key-value pair exists in a dictionary, i.e., if a dictionary has/contains a pair, use the in operator and the items() method. Specify a tuple (key, value) . Use not in to check if a pair does not exist in a dictionary.

How do you check if a key exists in a dictionary Python?

Check If Key Exists Using has_key() The has_key() method is a built-in method in Python that returns true if the dict contains the given key, and returns false if it isn't.

How would you retrieve a value for a key in a dictionary?

You can use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.

How do you check if a key exists in a list of dictionaries?

Method 1: Check If Key Exists using the Inbuilt method keys() Using the Inbuilt method keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use the if statement and the 'in' operator to check if the key is present in the dictionary or not.


2 Answers

One option if the number of keys is small is to use chained gets:

value = myDict.get('lastName', myDict.get('firstName', myDict.get('userName'))) 

But if you have keySet defined, this might be clearer:

value = None for key in keySet:     if key in myDict:         value = myDict[key]         break 

The chained gets do not short-circuit, so all keys will be checked but only one used. If you have enough possible keys that that matters, use the for loop.

like image 178
Peter DeGlopper Avatar answered Sep 19 '22 17:09

Peter DeGlopper


Use .get(), which if the key is not found, returns None.

for i in keySet:     temp = myDict.get(i)     if temp is not None:         print temp         break 
like image 25
TerryA Avatar answered Sep 19 '22 17:09

TerryA