How to merge two tuples in Python?

前端 未结 4 642
日久生厌
日久生厌 2020-12-29 19:53

How to convert the following tuple:

from:

((\'aa\', \'bb\', \'cc\'), \'dd\')

to:

(\'aa\', \'bb\', \'cc\', \'dd\')
         


        
相关标签:
4条回答
  • 2020-12-29 20:19
    >>> tuple(j for i in (('aa', 'bb', 'cc'), 'dd') for j in (i if isinstance(i, tuple) else (i,)))
    ('aa', 'bb', 'cc', 'dd')
    
    0 讨论(0)
  • 2020-12-29 20:28
    x = (('aa', 'bb', 'cc'), 'dd')
    tuple(list(x[0]) + [x[1]])
    
    0 讨论(0)
  • 2020-12-29 20:37
    l = (('aa', 'bb', 'cc'), 'dd')
    l = l[0] + (l[1],)
    

    This will work for your situation, however John La Rooy's solution is better for general cases.

    0 讨论(0)
  • 2020-12-29 20:39
    a = (1, 2)
    b = (3, 4)
    
    x = a + b
    
    print(x)
    

    Out:

    (1, 2, 3, 4)
    
    0 讨论(0)
提交回复
热议问题