0

I have the bellow list of tuples in python:

[(('A', 'B'), 4), (('B', 'C'), 4), (('C', 'D'), 4)]

And I want to extract the Information as shown bellow:

[('A', 'B', 4), ('B', 'C', 4), ('C', 'D', 4)]

How can I bring it to this form by extracting the inner tuple?

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
pjg
  • 33
  • 5

1 Answers1

1

You can use the unpack operator (prefix *) to spread out an iterable:

tuples = [(('A', 'B'), 4), (('B', 'C'), 4), (('C', 'D'), 4)]
result = [(*t[0], t[1]) for t in tuples]
result # [('A', 'B', 4), ('B', 'C', 4), ('C', 'D', 4)]
GBrandt
  • 685
  • 4
  • 11
  • Beat me to the punch. – kabanus Mar 18 '19 at 14:09
  • 1
    8 seconds too late my friend :^) – GBrandt Mar 18 '19 at 14:09
  • 1
    General comment - add some text, so it looks more like an answer. In particular naming the unpacking operator may be useful for future readers. – kabanus Mar 18 '19 at 14:09
  • @kabanus rather than trying to answer as quickly as possible to increase your rep, you both could be looking for an appropriate duplicate, which almost certainly exists ;) – Chris_Rands Mar 18 '19 at 14:14
  • @Chris_Rands I don't see how think quick answers would be a problem, specially if they do solve the problem. – GBrandt Mar 18 '19 at 14:19
  • @Chris_Rands This has been discussed extensively in meta, and likely you know is controversial. 1) there is no incentive in terms of fake internet points 2) its not always easy, and only very experienced users can find these quickly. Might not have been the case here, but I think you are fighting a losing battle. No harm in somebody gaining a bit of rep before you dupe-hammer the question. – kabanus Mar 18 '19 at 14:30
  • @Chris_Rands Also, in case it was not clear, thank you for your thorough moderation effort! Even if I'm the type to occasionally post a quick-and-dirty answer, I appreciate it. – kabanus Mar 18 '19 at 14:31