1

I have a list containing tuples like this:

list = [(('1', '6'), ('3', '4')), (((5, 6), (8, 6)), ((3, 6), (8, 1))), (('1', '6'), ('2', '5'))]

As you can see in the item: (((5, 6), (8, 6)), ((3, 6), (8, 1))) , tuples can contain tuples within them upto any level of depth.

I want to make a function called simplify_list such that each item in a list is simplified to a tuple containing only one layer. For example:

(('1', '6'), ('3', '4')) becomes ('1', '6', '3', '4')
(((5, 6), (8, 6)), ((3, 6), (8, 1))) becomes (5, 6, 8, 6, 3, 6, 8, 1)
(('1', '6'), ('2', '5')) becomes ('1', '6', '2', '5')

So end up with the following list as a result:

resulting_list = [('1', '6', '3', '4'), (5, 6, 8, 6, 3, 6, 8, 1), ('1', '6', '2', '5')]

I have made the following function but it doesnt seem to be working correctly, can anyone help me with that?

def simplify_list(list):
    simplified_list = []
    for item in list:
        simplified_list.extend(item)
    return simplified_list

Thanks in advance!

NOTE

My question is different from all of the other because it deals with tuples, not lists and no where on stackoverflow has been answered.

sshussain270
  • 1,785
  • 4
  • 25
  • 49
  • You can apply any of the solutions proposed [here](http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python) to every sublist. Duplicate? – alecxe Jan 18 '17 at 23:15

0 Answers0