0

If I have a list of lists and a dictionary with a list as a value:

ll = [['wed', 'thurs'], ['fri', 'sat', 'sun']]
d = {'week1':['mon', 'tues']}

how do I add additional lists to the same key? such that I get:

new_d = { 'week1': ['mon', 'tues'], ['wed', 'thurs'], ['fri', 'sat', 'sun'] }

or

new_d = { 'week1': (['mon', 'tues'], ['wed', 'thurs'], ['fri', 'sat', 'sun']) }

if I do:

 for item in ll:
    new_d['week1'].append(item)

I wind up with something like:

{'week1':['mon', 'tues', ['wed', 'thurs'], ['fri', 'sat', 'sun']] }

which is not what I want

kjarsenal
  • 934
  • 1
  • 12
  • 35
  • Your first expected output is *not valid Python syntax*. The commas are parsed as separators between different `key: value` pairs, but no `key:` portion is there. If you wanted that to be a tuple, you'll have to disambiguate the commas by putting `(..)` parentheses around the lists, like you did in your second example. – Martijn Pieters May 25 '16 at 17:15
  • 1
    Also, are you sure you don't want the value to be a list with nested lists, rather than a tuple? Tuples are really only meant to produce structure, like grouping together related information. Lists are for capturing a series of *homogenous* items, which is what you have here. See [What's the difference between list and tuples?](http://stackoverflow.com/q/626759) – Martijn Pieters May 25 '16 at 17:26
  • Yes, a list of lists would work best. – kjarsenal May 25 '16 at 17:31

2 Answers2

0

Build a new tuple holding the old as a nested list, plus the other two. Since you want the whole result to be a tuple, you'll have to convert ll to a tuple too:

new_d = {}
new_d['week1'] = (d['week1'],) + tuple(ll)

This is no different from building a tuple outside of a dictionary:

>>> ll = [['wed', 'thurs'], ['fri', 'sat', 'sun']]
>>> d = {'week1':['mon', 'tues']}
>>> d['week1']
['mon', 'tues']
>>> (d['week1'],) + tuple(ll)
(['mon', 'tues'], ['wed', 'thurs'], ['fri', 'sat', 'sun'])

If you want it to be a list instead of a tulpe, build that:

new_d['week1'] = [d['week1']] + ll
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I distilled more complex code to make the post as basic as possible and may have erred when I did so. Checking my logic now. In any event, building a new list/tuple is the key certainly the key. Thanks Martijn. – kjarsenal May 25 '16 at 17:49
  • @kjarsenal: glad to have been of help, and great that you found both answers to be useful to you! Unfortunately you can only mark one of the two posts as 'accepted', not both. Pick the one you feel helped you the most (and it is fine not to pick either). – Martijn Pieters May 25 '16 at 21:05
  • I know my vacillation was frustrating (apologies); just that both answers were very helpful. The crux of it (and I had to really deconstruct my function to determine) was that I needed my initial value to be a nested list so that the others could be appended inside of the nest. Also, I figured Austin could use the points more. Heartfelt thanks to both of you. – kjarsenal May 25 '16 at 21:48
  • No problem, the choice is indeed entirely up to you! I had just noticed the oscillation, and that's usually because a newer SO user doesn't know the accept mark is limited to one answer per question. :-) – Martijn Pieters May 25 '16 at 21:50
0

You've nearly got it. The crucial piece of information is that your value for the dictionary needs to be a single object.

In your example above, you are appending some list objects to the list, giving you the list you described which looked something like this:

[ 'mon',
  'tues',
  ['wed', 'thur'],
  ['fri', 'sat', 'sun']
]

So now you have to make a choice. If you want your dictionary's value to be a list of lists, then you would want to have your initial dictionary value be a list of lists, with just a single list within it. So that would change your example to be the following (note the double brackets there - a single list within a list):

d = {'week1':[['mon', 'tues']]}

Now when you append to the value, you will append new lists objects to the list that is the value, giving you a structure looking like the following instead:

[ ['mon', 'tues'],
  ['wed', 'thur'],
  ['fri', 'sat', 'sun']
]