Skip to content Skip to sidebar Skip to footer

Iterate Over Limited Dict_values In Python3

Using py3, I am trying to simply iterate over dict_values which is a view in python3 and not list, so I can't do dict_values[:limit] anymore. can't do below in py3, In [1]: large_

Solution 1:

Perhaps something along the lines of:

import string
d = dict([(c, ord(c)) for c in string.ascii_lowercase])
values = d.values()
g = iter(values)
subset = [next(g) for i in range(5)] # take the first five values
print(subset)

Post a Comment for "Iterate Over Limited Dict_values In Python3"