Guest User

Untitled

a guest
Jan 21st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # create a method to extract values from List
  2. def iterable(values):
  3. result_values = []
  4. for element in values:
  5. if isinstance(element, list):
  6. sub_iter_elements = iterable(element)
  7. result_values.extend(sub_iter_elements)
  8. else:
  9. result_values.append(element)
  10. return result_values
  11.  
  12. # create some test examples using a dict
  13. if __name__ == '__main__':
  14. test_dict = {}
  15. test_dict['A'] = [1,[2,3,[4,[5,[6,7,[8,[9,10,11]]]]]]]
  16. test_dict['B'] = [1,2]
  17. test_dict['C'] = [1,2,[3,4]]
  18. test_dict['D'] = 1
  19. # obtain every test case and run the method to extract values
  20. # in case you do not provide an array/list the stuff is going to return a message
  21. for test_value in test_dict.keys():
  22. iter_list = test_dict[test_value]
  23. if isinstance(iter_list, list):
  24. flatten_array = iterable(iter_list)
  25. print(flatten_array)
  26. else:
  27. print('You are not providing an array/list')
Add Comment
Please, Sign In to add comment