Home > OS >  Removing percentage from numbers in a list
Removing percentage from numbers in a list

Time:03-28

I have a list containing the numbers in the picture below:

enter image description here

I need to find a way to remove 30 percent off of every item in it and store them in a new list.

any help on getting this done?

CodePudding user response:

Here's Logic:-

  1. Iterate through list and store as float value and make a empty list above loop
  2. make another variable to get 30% of the stored value
  3. Subtract it
  4. use append() to insert the value in list(new)

CodePudding user response:

Consider x as our test data:

x = [120,
     100,
     90,
     0]

Solving:

Via iteration and appendage:

y = []

for number in x:
    # *0.7 is -30%
    y.append(x*0.7)

Via list comprehension

y = [i*0.7 for i in x]

Outputs:

[84.0, 70.0, 62.99999999999999, 0.0]

CodePudding user response:

items = [100, 200, 300]
iterator = map(lambda item: item-item*0.3 , items)
print(list(iterator))

this is equivalent to this code:

def remove_thirty_percent(item):   
return item-item*0.3

items = [100, 200, 300]
iterator = map(remove_thirty_percent , items)
print(list(iterator))

to learn more about Python map() function: https://www.geeksforgeeks.org/python-map-function/

CodePudding user response:

Two methods to solve it


def percent(mylist): newlist = [] for each in mylist: each = each - (each * 0.3) newlist.append(each) return newlist

lst = [130.00,153.00,221.00,117.00,55.00,200.00] print(percent(lst))

output

[91.0, 107.1, 154.7, 81.9, 38.5, 140.0]



def percent2(mylist): return [x-(x*0.3) for x in mylist]

lst = [130.00,153.00,221.00,117.00,55.00,200.00]

print(percent2(lst))


output

[91.0, 107.1, 154.7, 81.9, 38.5, 140.0]

  • Related