>>> lst = [1,2,4,5]
>>> map(lambda x: 'lower' if x < 3 else 'higher', lst)
['lower', 'lower', 'higher', 'higher']

Aside: It's usually preferred to use a list comprehension for this

>>> ['lower' if x < 3 else 'higher' for x in lst]
['lower', 'lower', 'higher', 'higher']
Answer from John La Rooy on Stack Overflow
🌐
Teclado
teclado.com › 30-days-of-python › python-30-day-20-map-filter
Day 20: map, filter, and Conditional Comprehensions | Teclado
In cases where the condition evaluates to True, the item is added; otherwise, it gets discarded.
🌐
Rafatieppo
rafatieppo.github.io › post › 2021_12_02_python_map_loop
Conditional in Python: map vs loop · Rafael Tieppo
December 2, 2021 - Since map() is written in C and is highly optimized, its internal implied loop can be more efficient than a regular Python for loop.
🌐
YouTube
youtube.com › code cube
Using conditions with Map - Python - YouTube
How to use conditions for map and lambda in Python. if - else condition should be used for mapping the elements of list.
Published: November 15, 2019
Views: 198
🌐
Real Python
realpython.com › lessons › mapping-conditions-handler-functions
Mapping Conditions to Handler Functions (Video) – Real Python
So, let’s come back to our original plan here, which was emulating a switch/case statement. So, now that we know about these first-class functions, what we can do here is actually create a function dictionary that maps from the condition name—or, from the actual condition to the handler function.
Published: March 2, 2019
🌐
HackerEarth
hackerearth.com › practice › notes › python-diaries-chapter-3
Python Diaries Chapter 3 Map | Filter | For-else | List Comprehension - Divyanshu Bansal
If conditional is optional to filter the elements. ... We can also use list comprehension to create multi-dimesional arrays, check the following link for the same https://docs.python.org/3/tutorial/datastructures.html · It applies the function ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 46994142 › make-the-conditional-maps
python - Make the Conditional maps - Stack Overflow
October 29, 2017 - Copyg = sns.FacetGrid(both, row="Major car", col="Major housetype", \ margin_titles=True, size=5) g.map(map_subset, "imd_score", db='imd_house_car') plt.tight_layout() plt.show() The imd_house_car is the joined table from imd_house and imd_car. The Python warned:map_subset() got multiple values for argument 'db'.
🌐
Real Python
realpython.com › python-map-function
Python's map(): Processing Iterables Without a Loop – Real Python
March 18, 2026 - You can use any kind of Python callable with map(). The only condition would be that the callable takes an argument and returns a concrete and useful value.
Top answer
1 of 3
16

Your conditions are sequential in nature; you want to test one after the other, not map a small number of keys to a value here. Changing the order of the conditions could alter the outcome; a value of 5 results in "greater than 0.5" in your sample, not "it is equal to 5".

Use a list of tuples:

myconditions  = [
    (lambda i: i > 0.5, "greater than 0.5"),
    (lambda i: i == 5, "it is equal to 5"),
    (lambda i: i > 5 and i < 6, "somewhere between 5 and 6"),
]

after which you can access each one in turn until one matches:

for test, message in myconditions:
    if test(i):
        return message

Re-ordering the tests will change the outcome.

A dictionary works for your first example because there is a simple equality test against multiple static values that is optimised by a dictionary, but there are no such simple equalities available here.

2 of 3
1

You can't use a dictionary to map arbitrary conditionals since more than one of them could be true at the same time. Instead you need to evaluate each one sequentially and execute the associated code the first time a true one is encountered. Here's an outline of one way to formally implement something like that which even allows the equivalent of a default: case.

from collections import namedtuple

Case = namedtuple('Case', ['condition', 'code'])

cases = (Case('i > 0.5',
            """print 'greater than 0.5'"""),

         Case('i == 5',
            """print 'it is equal to 5'"""),

         Case('i > 5 and i < 6',
            """print 'somewhere between 5 and 6'"""))

def switch(cases, **namespace):
    for case in cases:
        if eval(case.condition, namespace):
            exec(case.code, namespace)
            break
    else:
        print 'default case'

switch(cases, i=5)

Output:

greater than 0.5
🌐
Stack Overflow
stackoverflow.com › questions › 43726789 › how-to-add-conditional-if-using-maplambda-function-under-python
csv - How to add conditional 'if' using 'map(+lambda)' function under Python - Stack Overflow
May 2, 2017 - # The following 'map()' function calculates Overbooking number Overbooking_Number = map(lambda mu_,cs_,co_:np.ceil(poisson.ppf(co_/(cs_+co_),mu_)),mu,cs,co) data['Overbooking_Number'] = Overbooking_Number header = 'LOC_ID', 'Prod_Class_ID', 'Date', 'Avg_Noshow', 'Walk_Cost', 'Room_Rev', 'Overbooking_Number' # Write to an output file with open("output.csv",'wb') as resultFile: wr = csv.writer(resultFile,quoting=csv.QUOTE_ALL) wr.writerow(header) z = zip(data['LOC_ID'],data['Prod_Class_ID'],data['Date'],data['Avg_Noshow'],data['Walk_Cost'],data['Room_Rev'],data['Overbooking_Number']) for i in z: wr.writerow(i)
🌐
DataCamp
datacamp.com › tutorial › python-map-function
Python map() Function: A Complete Guide | DataCamp
December 10, 2025 - Often, you don't want to transform every item. You only want to transform items that meet a certain condition. This is a common pattern in data analysis, and it's easily solved by combining map() with Python's filter() function.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.map.html
pandas.Series.map — pandas 3.0.6 documentation
map accepts a dict or a Series. Values that are not found in the dict are converted to NaN, unless the dict has a default value (e.g.
🌐
EdrawMind
edrawmind.com › mind-maps › 16083 › conditional-statement-syntax-in-python
Conditional statement syntax in Python | Mind Map - EdrawMind
July 4, 2021 - A mind map about conditional statement syntax in python. You can edit this mind map or create your own using our free cloud based mind map maker.