fork download
  1. def get_diagonal_and_non_diagonal(L):
  2. '''(list of list of int) -> tuple of (list of int, list of int)
  3.  
  4. Return a tuple where the first item is a list of the values on the
  5. diagonal of square nested list L and the second item is a list of the rest
  6. of the values in L.
  7.  
  8. >>> get_diagonal_and_non_diagonal([[1, 3, 5], [2, 4, 5], [4, 0, 8]])
  9. ([1, 4, 8], [3, 5, 2, 5, 4, 0])
  10. '''
  11.  
  12. diagonal = []
  13. non_diagonal = []
  14. for row in range(len(L)):
  15. for col in range(len(L)):
  16.  
  17. if row == col:
  18. diagonal.append(L[row][col])
  19. if row != col:
  20. non_diagonal.append(L[row][col])
  21.  
  22. return (diagonal, non_diagonal)
  23.  
  24.  
  25. print(get_diagonal_and_non_diagonal([[1, 3, 5], [2, 4, 5], [4, 0, 8]]))
Success #stdin #stdout 0.04s 9072KB
stdin
Standard input is empty
stdout
([1, 4, 8], [3, 5, 2, 5, 4, 0])