Dict_Values to String in Python

Dict_Values to String in Python

I simply want to convert an object that looks like this:

dict_values(['baf0242b-d7fc-49d7-aada-220344969fb6'])

That I got from doing this:

dictionary.values()

To a simple string: 'baf0242b-d7fc-49d7-aada-220344969fb6'

How can I?

3

3 Answers

Say you have a dictionary:

d = {'key': 'baf0242b-d7fc-49d7-aada-220344969fb6'}

The result you see is from running the following:

d.values()

To get what you want, convert it to a list and get the first item on it:

d = {'key': 'baf0242b-d7fc-49d7-aada-220344969fb6'}
result = list(d.values())[0]

list(your_dict.values())[0]

As @rdas mentioned, dict_values is a view in Python 3+, and the first index will return your string.

If you are getting the first value, or the only value in your case. This might be a bit more memory efficient:

next(iter(d.values())))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.