GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Find the maximum values in a given heterogeneous list using lambda - w3resource

Python: Find the maximum values in a given heterogeneous list using lambda

Last update on July 12 2025 13:05:20 (UTC/GMT +8 hours)


29. Heterogeneous Max Finder Lambda

Write a Python program to find the maximum value in a given heterogeneous list using lambda.

Sample Solution:

Python Code :

# Define a function 'max_val' that takes a list 'list_val' as input
def max_val(list_val):
    # Find the maximum value in 'list_val' based on two criteria:
    # 1. First, sort by whether the element is an integer or not (True for integers, False for non-integers)
    # 2. Second, sort lexicographically by the elements themselves
    max_val = max(list_val, key=lambda i: (isinstance(i, int), i))
    
    # Return the maximum value found in the list
    return max_val

# Create a list 'list_val' containing a mix of strings and integers
list_val = ['Python', 3, 2, 4, 5, 'version']

# Print the original list 'list_val'
print("Original list:")
print(list_val)

# Find and print the maximum values in the list using the 'max_val' function and lambda expressions
print("\nMaximum values in the said list using lambda:")
print(max_val(list_val)) 

Sample Output:

Original list:
['Python', 3, 2, 4, 5, 'version']

Maximum values in the said list using lambda:
5

For more Practice: Solve these Related Problems:

  • Write a Python program to find the minimum value in a heterogeneous list using lambda, ignoring non-comparable types.
  • Write a Python program to determine the maximum string (lexicographically) in a heterogeneous list using lambda.
  • Write a Python program to find the longest element in a heterogeneous list based on length (for strings) or value (for numbers) using lambda.
  • Write a Python program to extract numeric values from a heterogeneous list and calculate their average using lambda.

Go to:

  • Python Lambda Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to sort a given list of lists by length and value using lambda.
Next: Write a Python program to sort a given matrix in ascending order according to the sum of its rows using lambda.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.