Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # create a method to extract values from List
- def iterable(values):
- result_values = []
- for element in values:
- if isinstance(element, list):
- sub_iter_elements = iterable(element)
- result_values.extend(sub_iter_elements)
- else:
- result_values.append(element)
- return result_values
- # create some test examples using a dict
- if __name__ == '__main__':
- test_dict = {}
- test_dict['A'] = [1,[2,3,[4,[5,[6,7,[8,[9,10,11]]]]]]]
- test_dict['B'] = [1,2]
- test_dict['C'] = [1,2,[3,4]]
- test_dict['D'] = 1
- # obtain every test case and run the method to extract values
- # in case you do not provide an array/list the stuff is going to return a message
- for test_value in test_dict.keys():
- iter_list = test_dict[test_value]
- if isinstance(iter_list, list):
- flatten_array = iterable(iter_list)
- print(flatten_array)
- else:
- print('You are not providing an array/list')
Add Comment
Please, Sign In to add comment