I’m python newbie and found something difficult while doing some practices.
I have few def functions under a class look like:
Class A(object):
def __init__(self):
self.itemID = []
self.itemlist = []
def add(self, ID, list):
self.itemID.append(ID)
self.itemlist.append(list)
def get_item(self,ID):
self.ID = ID
result = []
for self.ID in self.itemlist:
result.append(self.itemlist)
return result
I’m having problems with “def get_item”. Here’s an example..
if i do:
A.add('abc', 'bcd')
A.add('abc1', 'bcd1')
A.get_item('abc')
This should return abc, bcd but mine returns [[‘bcd’, ‘bcd1’], [‘bcd’, ‘bcd1’]]…
Apologise for the long complicated dumb looked codes…. Any feedbacks would be so appreciated.. Thanks
your get_item function is absolutely wrong 🙂
Here’s how it should look like:
Or even better:
Now, if I understand the logic behind your class, you want to be able to retrieve a list of object given a common ID. This can be better done using a dict