2

I have a nested list newlist. I want to find the sum of values like '£2.99','£4.99'. The effective sum would be like 2.99+4.99+2.99 etc

newlist = [['£2.99', '1', '16-Feb-19'], ['£4.99', '2', '16-Feb-19'], ['£2.99', '1', '15-Feb-19']....]
Praveen
  • 74
  • 1
  • 12
  • Can you elaborate and show a [mcve] that "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. Please see [ask] and [The perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). – Patrick Artner Feb 17 '19 at 12:41
  • 2
    Possible duplicate of [How do I sum the first value in each tuple in a list of tuples in Python?](https://stackoverflow.com/questions/638048/how-do-i-sum-the-first-value-in-each-tuple-in-a-list-of-tuples-in-python) – gold_cy Feb 17 '19 at 13:32

4 Answers4

4

Try this:

sum([float(i[0].replace('£','')) for i in a])

As @darksky mentioned, I will explain here a little more:

sum will returns the sum of one iterable item.

float will cast Strings to float like "2.99" --> 2.99

and Replace is obvious.

the output will be:

10.97
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
3

You can use a generator expression and sum the first element in each list (simply slice it with [1:] to skip the £):

my_list = [['£2.99', '1', '16-Feb-19'], ['£4.99', '2', '16-Feb-19'],
           ['£2.99', '1', '15-Feb-19']]


sum(float(l[0][1:]) for l in my_list)
# 10.97
yatu
  • 86,083
  • 12
  • 84
  • 139
3

Use list comprehensions to solve this:

l= [['£2.99', '1', '16-Feb-19'], ['£4.99', '2', '16-Feb-19'], ['£2.99', '1', '15-Feb-19']]

# 1. replace() will replace the first argument '£' with second argument (empty space).
# 2. map(): It takes two arguments, where first argument is the datatype 
#           you wish to convert to and second argument is value we want to convert.
# 3. list(): Since the list comprehension gives us the list of mapping, where as we need to 
#            convert it to proper proper list with numbers, and that's why we use list() 
#            to do so.
sum(list(map(float,[i[0].replace('£','') for i in l])))
    10.97
cph_sto
  • 7,189
  • 12
  • 42
  • 78
  • 2
    Same as other answer @cph_sto, `sum` can take a generator, no need to extract a list from the map. There's actually no need for the map at all, as you only have a string in each iteration having sliced with the first element – yatu Feb 17 '19 at 13:05
  • @yatu I noticed that. But, I would let it remain that way as other people could see that some of my steps were redundant, and they refrain from doing so. – cph_sto Feb 17 '19 at 13:07
2

Here's a different answer:

data = [['£2.99', '1', '16-Feb-19'], ['£4.99', '2', '16-Feb-19'], ['£2.99', '1', '15-Feb-19']]
sum = 0.0

for item in data:
    formatted = item[0][1:]
    sum += float(formatted)

After that code completes, sum will be equal to 10.97, the result.

PrinceOfCreation
  • 389
  • 1
  • 12