input: Write a function to convert a list of books into a dictionary, where each book is represented by a dictionary containing its title, author, and number of pages. If the input list is empty or contains invalid data, the function should return an empty list.
  assert books_to_dict([['The Hobbit', 'J.R.R. Tolkien', 295], ['The Catcher in the Rye', 'J.D. Salinger', 223]])==[{'title': 'The Hobbit', 'author': 'J.R.R. Tolkien', 'pages': 295}, {'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger', 'pages': 223}]
  assert books_to_dict([['1984', 'George Orwell', 328]])==[{'title': '1984', 'author': 'George Orwell', 'pages': 328}]
  assert books_to_dict([])==[]
  assert books_to_dict([['To Kill a Mockingbird', 'Harper Lee'], ['Pride and Prejudice', 'Jane Austen', 432]])==[{'title': 'Pride and Prejudice', 'author': 'Jane Austen', 'pages': 432}]
output: def books_to_dict(books):
      if not books:
          return []
      valid_books = []
      for book in books:
          if len(book) == 3:
              valid_books.append({'title': book[0], 'author': book[1], 'pages': book[2]})
      return valid_books