0

i have a list named as abc which have data like this :

{'devicetype': ['nokia'],'userid': ['1234'], 'year': ['2013']}

Now i have to generate the md5 of the values like nokia , '1234' , '2013' for this i had taken these value in variable like this

 devicetype = abc['devicetype']
 userid = abc['userid']
 year = abc['year']

after that i tried to use md5 to generate a hash like this

  authvalue = hashlib.md5()
  authvalue.update(devicetype+userid+year)

it gives me an error "must be string or buffer, not list" i know this will accept just string . but how can i generate the md5 of these list value?

2
  • what is auth1 ? You realise that the three variables you have are arrays, too, right? year == ['2013'] Commented Aug 4, 2013 at 19:11
  • @moopet : by mistake i put auth1 there . it is authvalue . And yes i know this is a array. how can i generate the hash of it Commented Aug 4, 2013 at 19:18

2 Answers 2

3

You have lists, not strings. Take the first element of each list:

authvalue = hashlib.md5()
auth1.update(devicetype[0] + userid[0] + year[0])
Sign up to request clarification or add additional context in comments.

Comments

1

Martijn Pieters' answer is basically correct, you have lists of a single element. If you have a big dictionary though, manually adding [0] to each entry might be a pain. So instead you can use map() and reduce() to do that for you.

If d is your dictionary with key-value pairs as above, you can do:

values = map(lambda x: x[0], d.values())

d.values() is just a list of the values of your dictionary (in your case, a list of 1-element long lists):

[['1234'], ['nokia'], ['2013']]

By mapping that lambda function to each one of them you get rid of the inner list:

['1234', 'nokia', '2013']

Then you can get the concatenation of all strings by reducing that list:

concat = reduce(lambda x, y: x + y, values, "")

So concat will be:

'1234nokia2013'

which then you can feed to your hashing function.

2 Comments

I'd use ''.join(values) instead of that whole reduce(lambda...) mess.
Well, that depends on how much of a functional programmer one is, I guess :)

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.