By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Quick Filter Dictionary

P: n/a
Hi,
I have a dictionary which may contain various keys/values, however,
it will always contain 'name' and 'age' keys.

This dictionary is kept inside a class, such as....

class Person:
def __init__(self, name, age):
self.data = {'name' : name, 'age' : age}

def getData(self, includeNameAge=False):
if includeNameAge: return self.data
tmp = <shallow or deep copy of 'data'>
del tmp['name']
del tmp['age']
return tmp

The getdata method should return the data stored for the
person, however, it should be able to return the data excluding the
'name' and 'age'. Any suggestions on how to best implement this?
Keep in mind this sample code is meant to be an example, I realize
that a person would probably always want the name and age returned.

thanks

Mar 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 14, 7:29 am, "abcd" <codecr...@gmail.comwrote:
Hi,
I have a dictionary which may contain various keys/values, however,
it will always contain 'name' and 'age' keys.

This dictionary is kept inside a class, such as....

class Person:
def __init__(self, name, age):
self.data = {'name' : name, 'age' : age}

def getData(self, includeNameAge=False):
if includeNameAge: return self.data
tmp = <shallow or deep copy of 'data'>
del tmp['name']
del tmp['age']
return tmp

The getdata method should return the data stored for the
person, however, it should be able to return the data excluding the
'name' and 'age'. Any suggestions on how to best implement this?
Keep in mind this sample code is meant to be an example, I realize
that a person would probably always want the name and age returned.

thanks

I guess one solution might be....

def getData(self, includeNameAge=False):
if includeNameAge: return self.data
return dict(filter(lambda item: item[0] not in ('name', 'age'),
self.data.items()))

any other suggestions, improvements?

Mar 14 '07 #2

P: n/a
abcd a écrit :
On Mar 14, 7:29 am, "abcd" <codecr...@gmail.comwrote:
>Hi,
I have a dictionary which may contain various keys/values, however,
it will always contain 'name' and 'age' keys.

This dictionary is kept inside a class, such as....

class Person:
Do yourself a favor : use new-style classes:
class Person(object):
> def __init__(self, name, age):
self.data = {'name' : name, 'age' : age}
You may improve this part with kwargs:

def __init__(self, name, age, **kw):
kw.update(name=name, age=age)
self.data = kw

Also, and FWIW, object attributes are stored in self.___dict__. Do you
have a compelling reason to use yet another 'data' dict ?
> def getData(self, includeNameAge=False):
if includeNameAge: return self.data
tmp = <shallow or deep copy of 'data'>
tmp = self.data.copy()
> del tmp['name']
del tmp['age']
return tmp
The problem with this implementation (as well as for the other below) is
that in the first case you return a reference to the data dict itself,
while in the second case you return a new dict.

p = Person('bibi', 42, foo='bar')
data = p.getData(True)
data['baaz'] = 'quux'
del data['name']
print p.data

p = Person('bibi', 42, foo='bar')
p.getData()
data['baaz'] = 'quux'
del data['foo']
print p.data

Better to *always* return a copy IMHO.
Mar 14 '07 #3

P: n/a
abcd wrote:
On Mar 14, 7:29 am, "abcd" <codecr...@gmail.comwrote:
>Hi,
I have a dictionary which may contain various keys/values, however,
it will always contain 'name' and 'age' keys.

This dictionary is kept inside a class, such as....

class Person:
def __init__(self, name, age):
self.data = {'name' : name, 'age' : age}

def getData(self, includeNameAge=False):
if includeNameAge: return self.data
tmp = <shallow or deep copy of 'data'>
del tmp['name']
del tmp['age']
return tmp

The getdata method should return the data stored for the
person, however, it should be able to return the data excluding the
'name' and 'age'. Any suggestions on how to best implement this?
Keep in mind this sample code is meant to be an example, I realize
that a person would probably always want the name and age returned.

thanks


I guess one solution might be....

def getData(self, includeNameAge=False):
if includeNameAge: return self.data
return dict(filter(lambda item: item[0] not in ('name', 'age'),
self.data.items()))

any other suggestions, improvements?
You might find

return dict((x,y) for (x, y) in self.data.iteritems()
if x not in ('name', 'age'))

easier to understand. Or you might not ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

Mar 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.