CloudyML - Data Science & Analytics
46.7K subscribers
260 photos
54 videos
33 files
1.1K links
My Assignment Driven Data Science Courses
https://linktr.ee/cloudyml

My LinkedIn profile
https://www.linkedin.com/in/akashraj001

My Instagram
https://www.instagram.com/cloudyml.akash
Download Telegram
#OUIZ👀

Given a list of numbers, return the second highest number in the list. If there are no second highest number, return None.

def second_highest(numbers):
# option 1
# numbers.sort(reverse=True)
# return numbers[1]
# option 2
# return sorted(list(set(numbers)))[-2]
# option 3
# return max(set(numbers), key=numbers.count)
# option 4
# return sorted(numbers)[-2]

numbers = [1, 2, 3, 4, 5, 5, 7]
print(second_highest(numbers))

➡️Output - 5

——————————————

Option 1: Using the sort() method to sort the list in descending order and then returning the second element in the list.

Option 2: Using the set() method to remove any duplicates from the list, then sorting the list and returning the second last element.

Option 3: Using the set() method to remove any duplicates from the list, then using the max() method with the key argument set to the count of each element in the list to return the second most common element in the list.

Option 4: Using the sorted() method to sort the list in ascending order and then returning the second last element in the list.
🆒17👏3