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'])
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']
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]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With