Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change elements from dictionary using split()?

I have the following dictionary:

things = { 0: ["32 64 43 12 67", "14 35 61 46 89"] 1: ["23 54 11 59 90", "56 6 91 11 19"] }

I want to iterate through the numbers and in order to do that I need to transform them into integers. I tried splitting the strings using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for z in range(len(things)):
 for c in things[z]:
    c = c.split(" ")

but it doesn’t change anything

>Solution :

You can transform your input to the required output using nested comprehensions.

output = {k: [[int(x) for x in s.split()] for s in v] for k, v in things.items()}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading