-3

Trying to create a dictionary of two keys and having more then one value.

myList = [[('2016-11-01', 'USD'), 'ECB News'],
         [('2016-11-01', 'USD'), 'FED News'],
         [('2016-11-02', 'EUR'), 'Brexit News'],
         [('2016-11-03', 'USD'), 'Yellen Speaking']]

myDict = defaultdict(lambda: defaultdict(list))
for d, value in myList:
    print(d, value)
    myDict[d].append(value)  #<<<----- Error here

print(myDict)

getting Error:

   myDict[d].append(value)
AttributeError: 'collections.defaultdict' object has no attribute 'append'

Expected output: Append same key pair values together.

{[('2016-11-01', 'USD'): 'ECB News', 'FED News'],
         [('2016-11-02', 'EUR'): 'Brexit News'],
         [('2016-11-03', 'USD'): 'Yellen Speaking']}
K S
  • 85
  • 1
  • 8
  • Possible duplicate of [How to add multiple values to a dictionary key in python?](http://stackoverflow.com/questions/20585920/how-to-add-multiple-values-to-a-dictionary-key-in-python) – TemporalWolf Nov 18 '16 at 21:55
  • You have a dictionary of dictionaries of lists. What you apparently wanted was a set of lists that are also somehow dictionaries? So yeah I think you're a bit stuck. Could you clarify with output that might actually be achievable? – jonrsharpe Nov 18 '16 at 21:56
  • Keying multiple values via a list: `[('2016-11-01', 'USD'): ['ECB News', 'FED News']]` is probably the solution you want. See the possible duplicate link. – TemporalWolf Nov 18 '16 at 21:58
  • @KS: ARe you sure you provided the right syntax of the desired output? Because it is not the `dict` nor the list. `:` in between doesn't makes sense – Moinuddin Quadri Nov 18 '16 at 21:58

3 Answers3

2

I think you want the dict object but you have mentioned invalid format as the required result. For getting the dict, you may use collections.defaultdict as:

from collections import defaultdict

new_dict = defaultdict(list)  # <-- You do not need `lambda` here
for k, v in myList:
    new_dict[k].append(v)

# `new_dict` holds the value:
# {('2016-11-02', 'EUR'): ['Brexit News'], 
# ('2016-11-01', 'USD'): ['ECB News', 'FED News'], 
# ('2016-11-03', 'USD'): ['Yellen Speaking']})

where myList is the list as mentioned in the question.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • That worked, thank you MQ – K S Nov 18 '16 at 22:02
  • If my answer was helpful, don't forget to [accept it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). You can upvote too - click to small triangle above `2` above accepting mark. Thanks – Moinuddin Quadri Nov 18 '16 at 22:14
0

you need to use lists, so that they can be appended:

myDict = {}    
for d, value in myList:
    myDict.setdefault(d,[]).append(value)

Ouptput myDict:

 {('2016-11-01', 'USD'): ['ECB News', 'FED News'],
  ('2016-11-02', 'EUR'): ['Brexit News'],
  ('2016-11-03', 'USD'): ['Yellen Speaking']}

setdefault() method creates an empty list if the key does not exist yet or returns already created list so you can append another value

Juraj Bezručka
  • 512
  • 5
  • 21
0

Are you trying to do this ?

from collections import defaultdict

myList = [(('2016-11-01', 'USD'), 'ECB News'),
      (('2016-11-01', 'USD'), 'FED News'),
      (('2016-11-02', 'EUR'), 'Brexit News'),
      (('2016-11-03', 'USD'), 'Yellen Speaking')]

myDict = defaultdict(list)
for d, value in myList:
    print(d, value)
    myDict[d].append(value)  #<<<----- Error here

print(myDict)