0

I have a list that have tuples with negative values say like this

vect=[(-x*3,-y*2) for x in [2,3,4] for y in [1,5,6]]

and i want to print the list with its absolute values like

[(6, 2), (6, 10), (6, 12), (9, 2), (9, 10), (9, 12), (12, 2), (12, 10), (12, 12)]

but i tried to get an output but got an error that

TypeError: bad operand type for abs(): 'tuple'

So I need a help or suggestions regarding this problem.

sacuL
  • 49,704
  • 8
  • 81
  • 106
Uday Gunjal
  • 3
  • 1
  • 2

2 Answers2

1

Using map

Ex:

vect=[(-x*3,-y*2) for x in [2,3,4] for y in [1,5,6]]
print([map(abs, i) for i in vect])     #Python3 --> print([list(map(abs, i)) for i in vect])

Output:

[[6, 2], [6, 10], [6, 12], [9, 2], [9, 10], [9, 12], [12, 2], [12, 10], [12, 12]]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

Using a simple list comprehension:

[(abs(i[0]), abs(i[1])) for i in vect]

# [(6, 2), (6, 10), (6, 12), (9, 2), (9, 10), (9, 12), (12, 2), (12, 10), (12, 12)]
sacuL
  • 49,704
  • 8
  • 81
  • 106