1
function getParams(data) { return { id: data && data.uuid } } 

So the above represents a common pattern in Javascript for accessing the items of an object.

What is the most commonly used equivalent practice in Python for accessing items of a dict?

Would it be like so?

def getParams(data): params = {} if data is not None and hasattr(data, "id"): params["id"] = data["id"] return params 

If not, what is the best practice? Thanks.

3
  • 2
    Do you want to access attributes (data.foo) or items (data['foo'])? JavaScript uses the same syntax for these, but they are distinct in Python. Commented Jan 2, 2016 at 7:55
  • I want to access items, sorry. Assume data is a python dictionary. Commented Jan 2, 2016 at 7:59
  • Do you really want to return a dictionary with the same key as you're searching in the original? Commented Jan 2, 2016 at 8:02

4 Answers 4

5

If you want to get a key from a dict without knowing if it's there, you can use the get method of dicts. It returns None (or a specific default value) if the key isn't there:

>>> x = {} >>> print(x.get('a')) None >>> print(x.get('a', 'default')) default 
Sign up to request clarification or add additional context in comments.

4 Comments

Gotcha. What is the traditional thing to do if I want to check an item in a nested dictionary? i.e. Javascript equivalent would be:
data && data.uuid && data.uuid.someAttribute
@RyanYu: You can use get multiple times, like data.get('uuid', {}).get('someAttribute', None). But also look around on this site for other people asking similar questions, there are many.
Although get is convenient, be aware that it can be noticeably slower than more verbose code that uses in.
3

If you mean attributes:

params['id'] = data.id if data else None 

If you mean items:

params['id'] = data.get('id') 

In both cases params['id'] will contain value or None.

2 Comments

What if 'id' does not exist in data? did you check that?
If 'id' doesn't exist in data, data.get('id') will return None.
0

Python dict is different from javascript.

def getParams(data): params = {} if data and 'id' in data.keys(): params["id"] = data["id"] return params 

2 Comments

If you want to check, that an item is in a dictionary, use 'id' in data. Key checking is a key feature of dicts.
What Daniel said. 'id' in data.keys() is wasteful because it has to construct the keys object. That's not so bad in Python 3 because it's a dictionary view object that accesses the underlying dict; and look-ups on dictviews are fast. But in Python 2 dict.keys() has to build a new list object, and lookups on a list involve a slow linear search rather than the fast O(1) lookup that a dictionary or set provides.
0

You can use get method of dict. A default value can also be specified, which could be used if the specified key do not exist.

data.get('id', None) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.