15,664,272 members
1.00/5 (1 vote)
See more:
Given two lists, write a function to return a list with the highest number

list1 = ["2021-11-15", "2021-11-10", "2021-11-08", "2021-05-06"]
list2 = ["2021-11-09", "2021-03-02", "2021-06-01"]

o/p: ["2021-11-15"]

What I have tried:

This is what i have tried so far,

Python
```def myMax(list1):

# Assume first number in list is largest and initially and assign it to variable "max"

max1 = list1[0]
# Now traverse through the list and compare each number with "max" value. #Whichever is largest assign that value to "max'.
for x in list1:
if x > max1 :
max1 = x

# after complete traversing the list
# return the "max" value
return max

<pre>def myMax(list2):

# Assume first number in list is largest and initially and assign it to variable "max"

max2 = list2[0]
# Now traverse through the list and compare each number with "max" value. #Whichever is largest assign that value to "max'.
for x in list2:
if x > max2 :
max2 = x

# after complete traversing the list compare the two max values and return the "max" value
if max1< max2:
return max2
else:
return max1```

I don't want to write two separate functions like this. i want to be able to compare it in one single function and return the highest date. I don't want to use any built-in function either. Should i be importing and datetime library?
Posted
Updated 25-Jul-22 5:18am
v3

## Solution 2

Your lists do not contain numbers, they contain dates. So while your code will work as long as the dates are all in the form "YYYY-MM-DD", if the format is changed you may get invalid answers. You can do it in a single function by finding the highest in the first list and then checking the second list to see if there is a higher one still.
Python
```for x in list1:
# find the highest value

for x in list2:
# do the same```

v3

## Solution 1

Create two variables in the main code: maxValue and MaxList.
Write a single function that accepts a list. In the function compare each value against the maxValue. If maxValue is the smallest, replace it with the new max , and set the maxList to the list you passed in.
In the main code, set the maxValue to the first item in List1, and maxList to List1.
Call the function twice, once with List1 and then with List2.

Regardless of which list the maximum was found in, it is in maxValue, and the list it came from is in maxList.

Try it on paper and you'll see what I mean.

v2