开发者

how to add an empty list to a tuple

开发者 https://www.devze.com 2023-02-22 13:27 出处:网络
I need to add an empty list to an existing tuple. example: >>> ([1],[1,2]) + ([]) ([1],[1,2],[])

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 tu开发者_高级运维ple([]) but that evaluates to () and nothing is added to the original tuple.

Thank you.


Use a one-element tuple:

([], )
#  ^


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.


Did you try

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


tuples are immutable so you need to make a new tuple

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

精彩评论

暂无评论...
验证码 换一张
取 消