Lelo
Lelo

Reputation: 335

two operations on each element of a list

I would like to achieve the following lines in just one line:

   list1 = [abs(x) for x in list1]
   list1 = list(map(lambda x:x-1, list1))

I tried

list1 = [abs(x) and x-1  for x in list1]

even though I don't think there is such syntax, but anyway it didn't work.

Upvotes: 0

Views: 32

Answers (2)

kevinkayaks
kevinkayaks

Reputation: 2726

list1 = list(map(lambda x: abs(x)-1,list1))

Interestingly this is slower than

list1 = [abs(x) - 1 for x in list1]

Anyone know why?

Upvotes: 1

pwxcoo
pwxcoo

Reputation: 3273

and is a logical operator in Python, it cannot be used in this condition.

why not try

[abs(x) - 1 for x in list1]

Upvotes: 2

Related Questions