-3

I have a list of arrays something like this:

  list:
       [[['23456', '23456']], [['459687', '201667'],['769687', '203416']]

How can I remove the the nested [] to have a list of list something like this:

   list:
       [['23456', '23456'], ['459687', '201667'],['769687', '203416']]

Any idea?

Spedo
  • 355
  • 3
  • 13
  • 1
    What have you tried so far? are there always only one element? is the first one supposed to be missing a closing bracket? – Sayse Apr 17 '19 at 16:10
  • They are always two elements in one sublist. I am trying different ways to convert them to make the list clear (from the nested brackets) that I can use it in a loop for a simple comparison. E.g., I want to check if a list with two elements (e.g., my list: ['23456', '23456']) is a member of a bigger list with multiple sublists like ({['23456', '23456'], ['459687', '201667'],['769687', '203416']]) . – Spedo Apr 17 '19 at 16:18
  • 1
    You might try: `sum(list, [])` – Mark Apr 17 '19 at 16:19
  • Possible duplicate of [How to make a flat list out of list of lists](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – Mark Apr 17 '19 at 16:25

1 Answers1

1
new_list = []
for sub_list in nested_list:
    if type(sub_list[0]) == list:
        for potential_list in sub_list:
            if type(potential_list) == list:
                new_list.append(potential_list)
    elif type(sub_list[0]) == str:
        new_list.append(sub_list)
    else:
        print(type(sub_list)) # if you get here, you have even more weird nesting than in your example

This will handle your example but won't handle nesting deeper than the example. If you need deeper nesting create a function similar to the following but use recursion

ekmcd
  • 172
  • 8