3

I need to add an empty list to an existing tuple.
example:

>>> ([1],[1,2]) + ([])  
([1],[1,2],[])

My problem is that python seems to evaluate ([]) to [] on which i can't use the + operator.

I tried tuple([]) but that evaluates to () and nothing is added to the original tuple.

Thank you.

2
  • 3
    of course, tuples are immutable. So you don't actually extend the original tuple. Apr 7, 2011 at 10:00
  • 1
    ([]) isn't a tuple, it's just [] enclosed in expression parentheses.
    – BoltClock
    Apr 7, 2011 at 10:02

4 Answers 4

16

Use a one-element tuple:

([], )
#  ^
7

Try

>>> ([1],[1,2])+([],)
([1], [1, 2], [])

Simply putting something in between parentheses makes it an expression. Add a comma at the end to mark it as a tuple.

3

Did you try

([1],[1,2]) + ([],) 
1

tuples are immutable so you need to make a new tuple

a=([1],[1,2])
b=a+([],)
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.