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?

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

yes, it's means you can translate "list_of_pairs = list_of_pairs.append(matchup)" to "list_of_pairs.append(matchup)". then try again.
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?
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 =
Thanks for this, I literally spent half a day staring at this trying to figure it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.