0

I am starting with a text file of values and after much processing I get it to the required format: a dict of list containing n number of tuples of tuple pairs.

Here are two examples of the dict contents:

(0, 5): [((0, 5), (0, 1)), ((0, 5), (0, -3))]
(1.0, 0): [((1, 1), (2, 2)),
            ((1, 1), (3, 3)),
            ((1, 1), (-2, -2)),
            ((2, 2), (1, 1)),
            ((2, 2), (3, 3)),
            ((2, 2), (-2, -2)),
            ((3, 3), (1, 1)),
            ((3, 3), (2, 2)),
            ((3, 3), (-2, -2)),
            ((-2, -2), (1, 1)),
            ((-2, -2), (2, 2)),
            ((-2, -2), (3, 3))]

What I want to do is unpack the contents of the tuple pair, leaving the 'inside' tuples intact, in place.

The key, value pairs, two of which are shown above, have varying length pairs of tuples within the list. NOTE The smallest and largest are shown above.

My end result should look like this:

(0, 5): [(0, 5), (0, 1), (0, 5), (0, -3)]
(1.0, 0): [(1, 1), (2, 2),
           (1, 1), (3, 3),
           (1, 1), (-2, -2),
           (2, 2), (1, 1),
           (2, 2), (3, 3),
           (2, 2), (-2, -2),
           (3, 3), (1, 1),
           (3, 3), (2, 2),
           (3, 3), (-2, -2),
           (-2, -2), (1, 1),
           (-2, -2), (2, 2),
           (-2, -2), (3, 3)]

NOTE 2 In place is not a requirement. I simply do not have further need of the dictionary with the contents formatted as tuples of tuple pairs.

I have found the obvious 'how to unpack a tuple' but nothing I have found is guiding me to where I need to be.

I was thinking something along the lines of:

for key, value in dic.items():
    dic[key] = <unpack and update\replace existing values>

FINAL NOTE I would prefer this be done without the use of external libraries. I want to get the whole thing working locally on my machine, then 'modify' it to run it in an online environment. I can't guarantee what is and isn't already in the environment, so I am trying to use just basic built in python.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
MarkS
  • 1,455
  • 2
  • 21
  • 36
  • 1
    I'm not quite sure what has been the difficulty when you tried to do this. Can't you loop over all the outer tuples for each key, append the inner tuples to a temporary list, and insert the list into the dictionary at the same key to replace the previous list of nested tuples? – mkrieger1 Aug 22 '21 at 14:24
  • 1
    Because I didn't realize I could just re-assign the new list value to the key that way. You forced me to re-think it. I just figured it out. Much obliged. – MarkS Aug 22 '21 at 14:49
  • Does this answer your question? [How to make a flat list out of a list of lists](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) -- I know it says "list of lists", but it works the same for a list of tuples. – wjandrea Aug 22 '21 at 18:36
  • @wjandrea, Thanks, but not really. I needed to unpack various length tuples of tuples and have the unpacked tuples still be the values of a dict. This was the way I solved it: for key, value in res.items(): temp_list = [] for tup in value: for t in tup: temp_list.append(t) res[key] = temp_list – MarkS Aug 22 '21 at 22:57
  • @Mark That's the exact same solution as the [top answer](https://stackoverflow.com/a/952952/4518341), but using for-loops instead of a comprehension, which is shorter. `for key, value in res.items(): res[key] = [t for tup in value for t in tup]` – wjandrea Aug 23 '21 at 00:03

0 Answers0