You have a list data = [1, 3, 5, 7, 9]. You need to add the numbers 2, 4, 6, 8 to the list such that the list remains sorted. Which approach will be most efficient?

  • data += [2, 4, 6, 8]
  • data.extend([2, 4, 6, 8])
  • data.insert(2, [2, 4, 6, 8])
  • data.sort()
The most efficient approach is to use 'data += [2, 4, 6, 8]' as it directly appends the sorted numbers to the list, maintaining the sorted order.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *