Change the sign of the number in list in python by R4R Team

Example-

list is l=[2,-3,4,-1]

Aim to obtain the this list [-2,3,-4,1]


program-

print("Enter number in list")
li=list(map(int,input().split()))

print("List is",li)

#first way
l=li
for i in range(len(l)):
l[i]=-l[i]

print("List become",l)

#second way

l=list(map(lambda x:-x,l))
print("List become",l)


output-

Enter number in list
1 -2 3 -4 1 6 -7
List is [1, -2, 3, -4, 1, 6, -7]
List become [-1, 2, -3, 4, -1, -6, 7]
List become [1, -2, 3, -4, 1, 6, -7]




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!