Home > Software engineering >  Append the values if it is more than something
Append the values if it is more than something

Time:10-04

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]

CodePudding user response:

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))

CodePudding user response:

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

  • Related