-1

How can I concatenate different tuples inside a list to a single tuple inside a list in Python?

Current form:

[('1st',), ('2nd',), ('3rd',), ('4th',)]

Desired form:

[('1st', '2nd', '3rd', '4th')]
martineau
  • 119,623
  • 25
  • 170
  • 301
elnino
  • 173
  • 2
  • 9
  • `[tuple([inner_tuple[0] for inner_tuple in list_of_tuples])]` where `list_of_tuples` is the list of tuples you posted. – aybry Aug 24 '20 at 21:22
  • `sum(your_list, ())` You can optionally put `[]` around it. – Klaus D. Aug 24 '20 at 21:26
  • 1
    @wjandrea: Personally, I'd count this as a duplicate of that question; aside from using the `tuple` constructor (possibly replacing an equivalent use of the `list` constructor), every technique there applies. – ShadowRanger Aug 24 '20 at 21:34
  • 1
    @KlausD.: For small inputs as in this example, that's fine, but it's a misuse of `sum`; it's a Schlemiel the Painter's algorithm, repeatedly concatenating larger and larger `tuple`s. Turns a `O(n)` operation into a `O(n²)` operation. – ShadowRanger Aug 24 '20 at 21:35
  • Please [edit] your question and add your own attempt at doing this. – martineau Aug 24 '20 at 21:39
  • @ShadowRanger I'd argue that the time typing more complex code (as below) will exceed the computing time ever used by it, if the number of items is not getting too large. ;) – Klaus D. Aug 24 '20 at 21:40

5 Answers5

3

What you're trying to do is "flatten" a list of tuples. The easiest and most pythonic way is to simply use (nested) comprehension:

tups = [('1st',), ('2nd',), ('3rd',), ('4th',)]

tuple(item for tup in tups for item in tup)

result:

('1st', '2nd', '3rd', '4th')

You can wrap the resulting tuple in a list if you really want.

EDIT:

I also like Alan Cristhian's answer, which is basically transposing a column vector into a row vector:

list(zip(*tups))
wjandrea
  • 28,235
  • 9
  • 60
  • 81
bj0
  • 7,893
  • 5
  • 38
  • 49
2

Seems like this will do it:

import itertools
tuples = [('1st',), ('2nd',), ('3rd',), ('4th',)]
[tuple(itertools.chain.from_iterable(tuples))]
0x5453
  • 12,753
  • 1
  • 32
  • 61
1
>>> l = [('1st',), ('2nd',), ('3rd',), ('4th',)]
>>> list(zip(*l))
[('1st', '2nd', '3rd', '4th')]

See also: Using the Python zip() Function for Parallel Iteration

Alan Cristhian
  • 614
  • 7
  • 8
  • 1
    This works in this case, but if the input tuples have more than one element, it'll behave totally different: `l = [('1st', 0), ('2nd', 1), ('3rd', 2), ('4th', 3)]` -> `[('1st', '2nd', '3rd', '4th'), (0, 1, 2, 3)]` – wjandrea Aug 24 '20 at 21:42
  • 1
    @wjandrea that's how a transpose works, if you might have more columns, you can explicitly grab the first row with `[:1]` – bj0 Aug 24 '20 at 22:41
0

Simple solution:

tups = [('1st',), ('2nd',), ('3rd',), ('4th',)]

result = ()
for t in tups:
    result += t

# [('1st', '2nd', '3rd', '4th')]
print([result])
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
-1

Here's another one - just for fun:

[tuple([' '.join(tup) for tup in tups])]
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • What's with the `' '.join(tup)`? That's not part of the question, and could lead to unexpected behaviour for tuples that have more than one item or don't contain all strings. – wjandrea Aug 24 '20 at 21:39
  • @wjandrea I don't see what your problem is. It gets the expected output to OP's input, but using a different approach. You may not like it, but downvoting it seems a little excessive. – Jack Fleeting Aug 24 '20 at 21:42