0

I've looked around at other posts on here for a solution to my problem but none of them seems to work for me. I want to append a tuple to (what starts out as) an empty list. I've checked my code and it looks like whenever I try to just append my tuple it turns the list into a NoneType object. Here's what I've got:

list_of_pairs = []
for player in standings:
    opponent = standings[standings.index(player) + 1]
    matchup = (player[0:2] + opponent[0:2])
    list_of_pairs = list_of_pairs.append(matchup)
    print "ADDED TO LIST: " + str(list_of_pairs)

Each player in the standings list contains a tuple with four elements. I've tried using index and re-assigning list_of_pairs = [list_of_pairs, matchup], but nothing seems to be returning the right thing (i.e. [(player),(next player), (another player), (etc.)]. Every time I print "added to list" I just get ADDED TO LIST: None. I've checked matchup as well and it's definitely storing the first two values of the respective players fine. Any ideas?

Dave
  • 3
  • 1
  • 2
  • Possible duplicate of [Why does append return none in this code?](http://stackoverflow.com/questions/16641119/why-does-append-return-none-in-this-code) – SiHa Jun 02 '16 at 11:31

1 Answers1

1

This is because appending an element to a list returns None, which you are then printing when you do:

list_of_pairs = list_of_pairs.append(matchup)
print "ADDED TO LIST: " + str(list_of_pairs)

For example:

>>> a = []
>>> b = a.append('hello')
>>> print a
['hello']
>>> print b
None
Farhan.K
  • 3,425
  • 2
  • 15
  • 26
  • yes, it's means you can translate "list_of_pairs = list_of_pairs.append(matchup)" to "list_of_pairs.append(matchup)". then try again. – zds_cn Jun 02 '16 at 11:15
  • So could I get round this by generating a new variable (let's say `appended_list`), and doing `appended_list = list_of_pairs.append(matchup)` instead? Would that then mean that `list_of_pairs` would actually contain the values needed while `appended_list` would return None? – Dave Jun 02 '16 at 11:21
  • Forget the above! I think I just realised what you mean! The problem is that I'm re-assigning the variable to itself when I'm appending it right? So the solution is just `list_of_pairs.append(matchup)` without `list_of_pairs = ` – Dave Jun 02 '16 at 11:27
  • Thanks for this, I literally spent half a day staring at this trying to figure it out! – Dave Jun 02 '16 at 16:03