Abdulg

Flatten list

Nov 6th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.29 KB | None | 0 0
  1. def flattenList(x):
  2.     finishedList = []
  3.     if isinstance(x,list):
  4.         for i in range(0,len(x)):
  5.             finishedList.extend(flattenList(x[i]))
  6.     else:
  7.         finishedList.append(x)
  8.     return finishedList
  9.    
  10.    
  11. nestedList = [2, 1, [[3, 5], 4, 0, [], [5], 2], 4, [1, 7], 6]
  12.  
  13. print(flattenList(nestedList))
Advertisement
Add Comment
Please, Sign In to add comment