Reputation: 131
I've been attempting to add a value to the end of a list inside a dictionary using a different dictionary. The assignment is a little more complicated, so I have simplified the problem down to these three lines, but keep getting None as a result.
finley = {'a':0, 'b':2, 'c':[41,51,61]}
crazy_sauce = {'d':3}
print finley['c'].append(crazy_sauce['d'])
Upvotes: 7
Views: 22624
Reputation: 12689
Your solution is correct, but :
Most functions, methods that change the items of sequence/mapping does return None: list.sort, list.append, dict.clear.
So just use print dict in next line after you update the list in dict.
finley = {'a':0, 'b':2, 'c':[41,51,61]}
crazy_sauce = {'d':3}
finley['c'].append(crazy_sauce['d'])
print(finley)
Upvotes: 1
Reputation: 14201
The append()
method changes the list in-place - it doesn't create and then return the changed (extended) list, it doesn't return anything (which in Python means that it returns the None
special value).
So you first change your nested list in-place (not assigning it to other variable):
finley['c'].append(crazy_sauce['d'])
and then print the changed outer directory:
print(finley)
Upvotes: 0
Reputation: 1443
You are attempting to print the return
value of the .append()
function, which is None
. You need to print the dict
value after the call to .append()
, like so:
finley = {'a':0, 'b':2, 'c':[41, 51 ,61]}
crazy_sauce = {'d':3}
finley['c'].append(crazy_sauce['d'])
print(finley['c'])
Where the output is the following list:
[41, 51, 61, 3]
Upvotes: 1
Reputation: 1943
Your code is fine but print finley['c'].append(crazy_sauce['d'])
prints none since finley['c'].append(crazy_sauce['d'])
returns none. you remove print here and add a print finley['c']
in next line
finley = {'a':0, 'b':2, 'c':[41,51,61]}
crazy_sauce = {'d':3}
finley['c'].append(crazy_sauce['d'])
print finley['c']
Upvotes: 6