Question: There might be a similar ask to this one in the past, however wasn't able to find the one that I was looking for,

```
Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry')]
Output = [('Icecream', ['Vanilla', 'Chocolate', 'Strawberry'])]

```

Basically, given a list of tuples, need to merge the tuples to form a tuple list without duplicates where the second element in each tuple must be a list.

The Input list might contain more items like below,

```
Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry'), ('Veggie', 'Carrot'), ('Milk', 'whole'), ('Milk', 'formula')]

Output = [('Icecream', ['Vanilla', 'Chocolate', 'Strawberry']), ('Veggie', ['Carrot']), ('Milk', ['whole', 'formula'])]

```

Answer: 
I think the best approach on your problem is to convert ***Input*** in to a *dictionary*.

```
Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry'), ('Veggie', 'Carrot'), ('Milk', 'whole'), ('Milk', 'formula')]

new_dict = {}
for item in Input:
    key, *values = item
    if key not in new_dict:
        new_dict[key] = []
    new_dict[key].append(*values)
print(new_dict)

```

Output:

```
{'Icecream': ['Vanilla', 'Chocolate', 'Strawberry'], 'Veggie': ['Carrot'], 'Milk': ['whole', 'formula']}

```

Using this approach, you can easily get the items you need:

```
icecream_flavors = new_dict["Icecream"]
print(icecream_flavors)

```

Output:

```
['Vanilla', 'Chocolate', 'Strawberry']

```

But if you really want a *list of tuples* just convert the *dictionary* to a *list*:

```
new_list = list(new_dict.items())
print(new_list)

```

Output:

```
[('Icecream', ['Vanilla', 'Chocolate', 'Strawberry']), ('Veggie', ['Carrot']), ('Milk', ['whole', 'formula'])]

```
A [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) is very handy to store these type of data structures. A list in the requested output can then easily be created from it using [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions).

If the ordering is not important, a `set` would be the most logical sub type, leading to the following code.

```py
from collections import defaultdict

Input =  = [
    ('Icecream', 'Vanilla'),
    ('Icecream', 'Chocolate'),
    ('Icecream', 'Strawberry'),
    ('Veggie', 'Carrot'),
    ('Milk', 'whole'),
    ('Milk', 'formula')
]

dd = defaultdict(set)
for key, value in Input:
    dd[key].add(value)
Output = [(key, list(values)) for key, values in dd.items()]

```

If ordering ***is*** important, a dictionary (with arbitrary values, such as `True`) could be used instead, since [dictionaries are insertion ordered](https://stackoverflow.com/a/39980744/10669875) since Python 3.7 (or CPython 3.6):

```py
dd = defaultdict(dict)
for key, value in Input:
    dd[key][value] = True
Output = [(key, list(values)) for key, values in dd.items()]

```

Output of the first is:

```py
[('Icecream', ['Chocolate', 'Strawberry', 'Vanilla']), ('Veggie', ['Carrot']), ('Milk', ['whole', 'formula'])]

```

Output of the latter is identical to the output in the question:

```py
[('Icecream', ['Vanilla', 'Chocolate', 'Strawberry']), ('Veggie', ['Carrot']), ('Milk', ['whole', 'formula'])]

```