I am trying to create a function that returns a dictionary with keys "even" and "odd" and an array of the even & odd values from a range. Here's my codes:
def dictionary_even_odd(x, y):
d = {}
for i in range(x, y+1):
if i % 2 == 0:
d[even].append(i)
else:
d[odd].append(i)
return d
you need to use defaultdict from collections, and need key of string 'even' and 'odd'
import collections
def dictionary_even_odd(x, y):
d = collections.defaultdict(list)
for i in range(x, y+1):
if i % 2 == 0:
d['even'].append(i)
else:
d['odd'].append(i)
return d