1

edit: the second link solved my problem, thank you!

I have a global variable "messages", my keys will be "channel" and my values will be "message"

I have tried:

messages = {}
if channel not in messages:
    messages[channel] = message
else:
    messages[channel].append(message)

and:

messages = {}
if channel not in messages:
    messages[channel][0] = message
else:
    messages[channel].append(message)

How do I create a new dictionary key and append values to it?

I'm sorry if this is a repeat question, I looked hard but I couldn't figure it out.

  • 1
    @coldspeed, that dupe is a stretch. – Stephen Rauch Dec 16 '18 at 05:14
  • I was about to provide an answer but it is closed. I will provide it here. `messages = {}; if channel not in messages: messages[channel] = [message]; else: messages[channel].append(message)` You simply need to enclose the initial message in `[]` for it to start the list – ycx Dec 16 '18 at 05:22
  • @StephenRauch Ah, thanks for adding the more accurate one! – cs95 Dec 16 '18 at 05:30

0 Answers0