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: Sort a given mixed list of integers and strings using lambda - w3resource

Python: Sort a given mixed list of integers and strings using lambda

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


47. Sort Mixed List Lambda

Write a Python program to sort a given mixed list of integers and strings using lambda. Numbers must be sorted before strings.

Sample Solution:

Python Code :

# Define a function 'sort_mixed_list' that sorts a mixed list of integers and strings
def sort_mixed_list(mixed_list):
    # Sort the 'mixed_list' using the 'sort' method with a key function
    # The key function first checks if an element is a string (True for string, False for integer) and then sorts accordingly
    mixed_list.sort(key=lambda e: (isinstance(e, str), e))
    
    # Return the sorted mixed list
    return mixed_list

# Create a mixed list containing integers and strings
mixed_list = [19, 'red', 12, 'green', 'blue', 10, 'white', 'green', 1]

# Print the original mixed list
print("Original list:")
print(mixed_list)

# Sort the mixed list using the 'sort_mixed_list' function and print the sorted result
print("\nSort the said mixed list of integers and strings:")
print(sort_mixed_list(mixed_list)) 

Sample Output:

Original list:
[19, 'red', 12, 'green', 'blue', 10, 'white', 'green', 1]

Sort the said  mixed list of integers and strings:
[1, 10, 12, 19, 'blue', 'green', 'green', 'red', 'white']

For more Practice: Solve these Related Problems:

  • Write a Python program to sort a mixed list by placing numbers (sorted in ascending order) before strings sorted by length using lambda.
  • Write a Python program to sort a mixed list with numbers in descending order followed by strings in alphabetical order using lambda.
  • Write a Python program to sort a mixed list by ordering negative numbers first, then positive numbers, and finally strings using lambda.
  • Write a Python program to sort a mixed list by converting numbers to strings and sorting them lexicographically using lambda.

Go to:

  • Python Lambda Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to find index position and value of the maximum and minimum values in a given list of numbers using lambda.

Next: Write a Python program to sort a given list of strings(numbers) numerically 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.