abcd wrote: > On Mar 14, 7:29 am, "abcd" <[EMAIL PROTECTED]> wrote: >> 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 -- http://mail.python.org/mailman/listinfo/python-list