Reputation: 5
I've got a very simple question regarding creation of lists and appending items on the same line. I have been writing a Python Program for a school project (implementing an AI to play Blackjack) and would like to be able to do something like this:
hands = [[2,3]].append([4,5])
I need to be able to create new hands and add them to the AI players attributes for if it chooses to split, and I thought this would work but results in hands being None. I can certainly split it into two lines, but I feel like it would be more pythonic to do it within one line. I do not see why this doesn't work, if anyone can explain why it doesn't that would be great. What I read it as is that hands will be set to list [[2,3]] with element [4,5] appended to it.
Upvotes: 0
Views: 913
Reputation: 73498
append
mutates the list that it is called on, but returns None
. So assigning the return value to a variable makes little sense. One option is:
hands = [[2, 3]]
hands.append([4, 5])
or in one line (you seem to know the lists beforehand):
hands = [[2, 3], [4, 5]]
# Or
# hands = [[2, 3]] + [[4, 5]]
Upvotes: 1