0
codes = [('A', ['B', 'C']), ('D', ['E', 'C'])]

Output:

['A', 'B','C','D','E']

Tried Code:

n = 1
e = [x[n] for x in codes]
Alpha Beta
  • 39
  • 8
  • Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) – SuperStormer Sep 09 '22 at 08:34
  • then [Get unique values from a list in python](https://stackoverflow.com/q/12897374) – SuperStormer Sep 09 '22 at 08:35

2 Answers2

0

For the basic case shown there:

tuples = [("A", ["B", "C"]), ("A", ["B", "C"])]

unpacked = []

for current_tuple in tuples:
    # add the first element
    unpacked.append(current_tuple[0])
    # append all elements in the list to the tuple
    unpacked.extend(current_tuple[1])

for nesting of unknown depth

def get_all_elements(from_list) -> list:
    elements = []
    for item in from_list:
        if type(item) == list or type(item) == tuple:
            elements.extend(get_all_elements(item))
        else:
            elements.append(item)
            
    return elements

If all elements have a known and predictable structure e.g. 1st element is a string and 2nd element is a tuple of strings then the first case can be used. Otherwise the more complicated recursive function (second case) is required.

Pioneer_11
  • 670
  • 4
  • 19
0
from itertools import chain
list(set(list(chain.from_iterable([val for sublist in list(map(list, zip(*codes))) for val in sublist]))))
R. Baraiya
  • 1,490
  • 1
  • 4
  • 17