yurib
yurib

Reputation: 8147

how to add an empty list to a tuple

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.

Upvotes: 3

Views: 643

Answers (4)

tuples are immutable so you need to make a new tuple

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

Upvotes: 1

user515656
user515656

Reputation:

Did you try

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

Upvotes: 3

MAK
MAK

Reputation: 26586

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.

Upvotes: 7

Fred Foo
Fred Foo

Reputation: 363607

Use a one-element tuple:

([], )
#  ^

Upvotes: 16

Related Questions