Convert value of dictionary as a list?

Question:

Let’s say I was given with this text age_gender.txt

Female:18,36,35,49,19
Male:23,22,26,26,26

Here’s the code I have so far

file = open("age_gender.txt")
   contents = file.read().splitlines()
   new_dictionary = dict(item.split(":") for item in contents)

return new_dictionary

When I call the function readfile() this is the output I get, however the list of value is still in quotation marks. How do you convert that each value into a list?

{'Female': '18,36,35,49,19', 'Male': '23,22,26,26,26'}

The output I want to achieve is something like this

{'Female': [18,36,35,49,19], 'Male': [23,22,26,26,26]}
Asked By: Co.Coo

||

Answers:

>>> a
'Female:18,36,35,49,19,19,40,23,22,22,23,18,36,35,49,19,19,18,36,18,36,35,12,19,19,18,23,22,22,23'
>>> a.split(':')
['Female', '18,36,35,49,19,19,40,23,22,22,23,18,36,35,49,19,19,18,36,18,36,35,12,19,19,18,23,22,22,23']
>>> a.split(':')[1].split(',')
['18', '36', '35', '49', '19', '19', '40', '23', '22', '22', '23', '18', '36', '35', '49', '19', '19', '18', '36', '18', '36', '35', '12', '19', '19', '18', '23', '22', '22', '23']
>>> new_dictionary = dict({a.split(':')[0]:map(int,a.split(':')[1].split(','))}) 
>>> new_dictionary
{'Female': [18, 36, 35, 49, 19, 19, 40, 23, 22, 22, 23, 18, 36, 35, 49, 19, 19, 18, 36, 18, 36, 35, 12, 19, 19, 18, 23, 22, 22, 23]}

Applying that to Your code:

file = open("age_gender.txt")
   contents = file.read().splitlines()
   new_dictionary = dict()
   for item in contents:
       tmp = item.split(':')
       new_dictionary[tmp[0]] = list(map(int, tmp[1].split(',')))

return new_dictionary
Answered By: Tony Babarino

You need to split this dictionary value by “,” and then map it to int:

s['Female'] = map(int, s['Female'].split(','))
Answered By: ig-melnyk

You’ve go the basics down, the remaining steps are:

  • splitting the value on commas split(',')
  • Converting the string into an integer int(i)

Wrap these steps in a for loop and do it for every key/value pair the dictionary.

for key, value in new_dictionary.items():
    new_dictionary[key] = [int(i) for i in value.split(',')]
Answered By: Thomas Zumsteg

Here is another way to do it using ast.literal_eval to convert the ages into a Python list. It has the advantage of supporting all basic data types, e.g. float, without explicit conversion:

from ast import literal_eval

with open('age_gender.txt') as f:
    d = {gender: literal_eval(ages) for gender, ages in (line.split(':') for line in f)}

This will produce a dictionary with tuples as values:

{'Male': (23, 22, 26, 26, 26), 'Female': (18, 36, 35, 49, 19)}

If you really, really need lists, you can convert the tuples:

with open('age_gender.txt') as f:
    d = {gender: list(literal_eval(ages)) for gender, ages in (line.split(':') for line in f)}
{'Male': [23, 22, 26, 26, 26], 'Female': [18, 36, 35, 49, 19]}
Answered By: mhawke
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.