1

I am trying to pass a list of hex char, into a lambda function, reduce to calculate a total decimal value. I am not sure what I am doing wrong but the python interpreter wouldn't recognize list(enumerate(reversed(numList)) as a list of tuples.

  numList =  ['3', '0', 'e', 'f', 'e', '1']
  reduce(lambda sum,(up,x):sum+ int(x,16)*16**up,
         enumerate(reversed(numList)))

when I print out

  list(enumerate(reversed(numList)) 

It is a list of tuples.

  [(0, '1'), (1, 'e'), (2, 'f'), (3, 'e'), (4, '0'), (5, '3')]

But it spit our error: can only concatenate tuple (not "int") to tuple

UPDATE:

The code is now working with a minor addition ",0" added to the lambda

    reduce(lambda sum,(up,x):sum+ int(x,16)*16**up,
         list(enumerate(reversed(numList))),0)

I don't understand what that means. Also I am not sure what is the best way to approach this.

that means you make sure, that it starts with 0 instead of the first Argument - in this case (0,'1') - because otherwise the types dont match? – am2 1 min ago

.

the third argument you add is initializer. without it, the sum in first iteration will be (0,'1'). so you were trying to evaluate (0,'1')+int(x,16)*16**up which is invalid. – ymonad 14 mins ago

UPDATE 2:

reduce(lambda sum,(up,x):sum+ int(x,16)*16**up,enumerate(reversed(numList)),0)

is just as good and enumerate() returns iter and list(enumerate...) is redundant.

Marked it as solved.

5
  • 1
    the third argument you add is initializer. without it, the sum in first iteration will be (0,'1'). so you were trying to evaluate (0,'1')+int(x,16)*16**up which is invalid. docs.python.org/2/library/functions.html#reduce Commented Oct 21, 2016 at 6:29
  • ah ok. the message is saying that I am trying to add the int into the tuple. (I was thinking the other way around.) I got it now, thanks. Commented Oct 21, 2016 at 6:40
  • 1
    that means you make sure, that it starts with 0 instead of the first Argument - in this case (0,'1') - because otherwise the types dont match? Commented Oct 21, 2016 at 6:41
  • One quick question, is this enumerate(list) a proper way to extract the index of a list when using lambda functions? What are the other options I have? Commented Oct 21, 2016 at 6:54
  • I have added Pham's answer you intended to mark as the correct answer. Commented Oct 21, 2016 at 7:17

1 Answer 1

2

You don't need to use the generic reduce function when all you really need is to calculate the sum.

This works and is vastly simpler:

sum( int(x,16)*16**up for up,x in enumerate(reversed(numList)) )

Also, I'm going to guess you already know you can do the exact same thing like this:

int(''.join(numList), 16)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip. I was actually exploring how to use lambda function properly. If no other better answer, I will accept this as final answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.