0

Lets say I have a dict with key= 'keys'

>>> keys
'taste'

After a few lines..output

>>> {'taste': ('sweet', 'sour', 'juicy', 'melon-like')}

This code snippet

from collections import defaultdict
agent=defaultdict(str)
key_list=[]

key_list=[(keys,tuple(key_list))]
agent=dict(key_list)
#agent[keys]+=key_list

What I want to know is, is there a way to lets say I have agent= {'taste': ('sweet', 'sour', 'juicy', 'melon-like')}

I want to add to a list

key_list=['yuck!','tasty','smoothie']

and agent.setdefault('taste',[]).append(key_list)

and have output like:

 {'taste': ('sweet', 'sour', 'juicy', 'melon-like','yuck!','tasty','smoothie')}

instead of

{'taste': ('sweet', 'sour', 'juicy', 'melon-like',['yuck!','tasty','smoothie'])}

Is there a way to that?

Inshort:

  1. I want to add a list to an existing list,which is a value to a key in a dictionary (w/o iterations to find that particular key)
  2. Check if the element being fed in as a list already contains that element in that list which is a value to a particular key, say 'taste' here (could be string, as here)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2290820
  • 2,709
  • 5
  • 34
  • 62
  • What you have there is a dict of tuples, which are less convenient to append to than lists. Lists have a method `extend` which adds the individual elements of an additional list instead of adding the list itself. – Russell Borogove May 13 '13 at 20:43
  • @RussellBorogove Can you give an example in the answers? – user2290820 May 13 '13 at 20:46

4 Answers4

4

Check this out:

>>> tst =  {'taste': ('sweet', 'sour', 'juicy', 'melon-like')}
>>> tst.get('taste', ()) #default to () if does not exist.  
('sweet', 'sour', 'juicy', 'melon-like')
>>> key_list=['yuck!','tasty','smoothie']
>>> tst['taste'] = tst.get('taste') + tuple(key_list)
>>> tst
{'taste': ('sweet', 'sour', 'juicy', 'melon-like', 'yuck!', 'tasty', 'smoothie')}

To retrieve,

>>> tst = {'taste': ('sweet', 'sour', 'juicy', 'melon-like', 'yuck!', 'tasty', 'smoothie')}
>>> taste = tst.get('taste')
>>> taste
('sweet', 'sour', 'juicy', 'melon-like', 'yuck!', 'tasty', 'smoothie')
>>> 'sour' in taste
True
>>> 'sour1' in taste
False
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • you mean instead of get(), tst.setdefault(key,[]).extend(key_list) would that work? – user2290820 May 13 '13 at 20:47
  • why do you want to extend? can you not just replace ? that is what `tst.get('taste') + tuple(key_list)` is doing for you . – karthikr May 13 '13 at 20:48
  • in my code agent.setdefault(keys,[]).extend(key_list) worked Didnt use defaultdict as I didnt know what is agent-defaultdict(type???) I am extend to same list if list in same key. – user2290820 May 13 '13 at 21:03
  • Sure, In the question you showed a tuple, so i used that to illustrate. As long as it works, its all cool :) – karthikr May 13 '13 at 21:05
  • Either way setdefault/defaultdict, using lists, it generates duplicates. I would also like to know how can we search for an element in a list as value of a key? you can provide a code eg in the answer box – user2290820 May 13 '13 at 21:06
1

Ok so you have three questions here, let's go over them:

  1. You can extend a list to append elements from another list:
    [1,2,3].extend([4,5]) # [1,2,3,4,5]

  2. Since you have tuples, which are immutable, you can simply add a tuple to existing one:
    (1,2,3) + (4,5) # (1, 2, 3, 4, 5)

  3. If you do not want duplicates, you want to use set, and you can union them: {1,2}.union({2,3}) # set([1,2,3])
    see how 2 is not duplicated here. But beware, sets do not keep their order.

In the end, if you want to remove duplicates and don't care about order, you can combine 2 and 3:
set(old_value).union(set(new_value))

Otherwise, if you need to preserve order, see this question: Combining two lists and removing duplicates, without removing duplicates in original list

Community
  • 1
  • 1
Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
  • 1
    +1 for multiple solutions and only showing how they work, not coding it for him. Clearly the best answer here, imo. –  May 13 '13 at 20:53
  • @Dmitry Does that work for strings as well, instead of integers? – user2290820 May 13 '13 at 21:07
0

you want the extend function:

In [6]: l = [1,2,3]

In [7]: l2 = [4,5]

In [9]: l.extend(l2)

In [10]: l
Out[10]: [1, 2, 3, 4, 5]

but you will need the value of your key to be a list rather than a tuple

Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31
  • OP shows a dict of tuples instead of lists, so that won't work out of the box. `tuple( list( key_list ).extend( additionalItems ) )` gets closer. – Russell Borogove May 13 '13 at 20:42
  • @RussellBorogove commented an example above in karthikr 's comment. Is that what you are trying to say? – user2290820 May 13 '13 at 20:48
0

You are replacing agent, so the first defaultdict is never used. Also, if you change the values, tuples are not the way to go, as they are immutable.

Start out with

from collections import defaultdict
agent = defaultdict(list)

and then

agent[key] += 'item' 

or

agent[key].extend(list_of_items)
Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25