0

I have a dictionary like this

dict_a = {}
dict_a["key1"] = [["1","2"]]

i want to concatenate the value [["1","2"]] inside this dictionary with a string 3. So I did something like this:

new_list = list(dict_a["key1"])
dict_a["key1"] = new_list.append("3")

when i print the dictionary with the key key1 it gives me a None instead of [["1","2"], "3"]

print dict_a["key1"]

Cane someone explain why I got a "None"?

Hello_World
  • 101
  • 3
  • 10

2 Answers2

2

Yes, I can.

There are essentially 2 kinds of methods:

  1. Those which create and return a new object.

  2. Those which modify the object operated on in-place. Instead of returning self (which would in some situations quite handy), they return None by convention.

list.append() is one of the latter and thus returns None.

So you should replace

dict_a["key1"] = new_list.append("3")

with

new_list.append("3")
dict_a["key1"] = new_list

.

glglgl
  • 89,107
  • 13
  • 149
  • 217
1

The append() method doesn't return any value, so by default it returns None. If you assign its result to a variable, it will be None. That's why you get None if you print that value.

drekyn
  • 438
  • 2
  • 7