Kevvv
Kevvv

Reputation: 239

How to add items to a dictionary

people. I'm python newbie. I have two def functions as below under a class.

def add_item(self, itemID, itemlist):
    lines = []

    self.itemID = itemID
    self.itemlist = itemlist

    for line in self.itemID, itemlist:
        lines.append(line)

and

def get_keys(self):
    i = []
    i.append(self.itemID)
    return i 

If I do

example.add_item('abc', item list)
example.add_item('abcd', item list)
example.add_item('abce', item list)

then when I do

example.get_keys()

It should give:

['abc', 'abcd', 'abce']

but mine only gives the latest one that is ['abce'].

Can anyone please let me know how to fix?

Upvotes: 0

Views: 830

Answers (7)

Pierre GM
Pierre GM

Reputation: 20359

If I understand correctly, you want to add several couple of key and item_list to your example, and be able to retrieve the keys you added so far ? The easiest is to store the keys and the itemlist in two lists

Assuming that you initialize your object as such

def __init__(self, *args, **kwargs):
   self.itemID = []
   self.itemlist = []
   ...

Now, your add_item can simplify in

def add_item(self, itemID, itemlist):
    self.itemID.append(itemID)
    self.itemlist.append(itemlist)

and your get_key is only:

def get_keys():
    return self.itemID

  • Note that the get_key is exactly the one you have suggested, just simpler (no need to create a temporary list).

  • When you do

    lines = []
    for line in self.itemID, itemlist:
        lines.append(line)
    

    line first takes the value self.itemID, then itemlist. Eventually, your lines is just [self.itemID, itemlist]. Probably not what you had in mind.

Upvotes: 3

LarsVegas
LarsVegas

Reputation: 6832

First thing I see: You are iterating over two elements at once which is usually done by using zip(), at least if both elements are lists. Otherwise just use the container you want to loop over.

for id,line in zip(self.itemID, itemlist):
    lines.append(line)

But I don't see any dict...

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213401

def add_item(self, itemID, itemlist):
    lines = []

You are initializing your lines variable with empty list...

So, each time you invoke this method, it create a new list, and add the item to it.. You can rather return your lines from this method and store it in some variable where you are invoking this method..

Or, just declare lines as instance variable.

example = Example();
example.lines = []
example.lines.extend(example.add_item(itemId1, item_list1));
example.lines.extend(example.add_item(itemId2, item_list2));

Or, you can rather add your itemId and list to dictionary __dict__ of your class..

dict[itemId] = value;

** NOTE: - Just saw that, you have not used your for-loop correctly.. You don't iterate over two iterable like this..

You need to go through a good Python Book.. Or rather, Python Documentation..

Upvotes: 0

oz123
oz123

Reputation: 28878

You could also use the built-in method update:

example.update({'Key':'value'})

Upvotes: 0

selllikesybok
selllikesybok

Reputation: 1225

It looks like you are overwriting the item each time you add it.

When you call add_item, you are creating this variable "lines" that is never used again, and item_id and item_list are over-written with the new inputs.

Upvotes: 0

Jakob Bowyer
Jakob Bowyer

Reputation: 34718

Perhaps

i.extend(self.itemID)

Might be what you are looking for

Upvotes: 0

vinnydiehl
vinnydiehl

Reputation: 1704

To add a new key to a dictionary, just assign it.

dict['new_key'] = 'value'

Upvotes: 0

Related Questions