Append the values if it is more than something

Question:

I want to append the number in A to 5 if it is more than 5.

A = [1,2,3,4,5,6,7,8,9,10]

To something like this:

A = [1,2,3,4,5,5,5,5,5,5]

Asked By: Vui Chee Chang

||

Answers:

You can try using map

A = [1,2,3,4,5,6,7,8,9,10]
list(map(lambda x: x if x<5 else 5, A))
Answered By: Deepak Tripathi

You can use the min function to take the smaller of each list item and 5:

[min(i, 5) for i in A]

Demo: https://replit.com/@blhsing/InsidiousDigitalIntegrationtesting

Answered By: blhsing
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.