Reputation: 25
I have a dictionary:
{'my_account': [45010045, 43527907, 45147474, 35108100, 45159973],
'your_account': [38966628, 28171579, 39573751, 41359842, 42445236],
'his_account': [44822460, 45010045, 39276850, 39896128, 45265335]
}
I want to keep the first 2 elements of every key, so the result would look like:
{'my_account': [45010045, 43527907],
'your_account': [38966628, 28171579],
'his_account': [44822460, 45010045]
}
Is there any way to achieve this? Thanks.
Upvotes: 1
Views: 832
Reputation: 3555
using dictionary comprehension
my_dict = {'my_account': [45010045, 43527907, 45147474, 35108100, 45159973],
'your_account': [38966628, 28171579, 39573751, 41359842, 42445236],
'his_account': [44822460, 45010045, 39276850, 39896128, 45265335]
}
new_dict = {k:v[:2] for k,v in my_dict.items()}
# {'my_account': [45010045, 43527907], 'your_account': [38966628, 28171579], 'his_account': [44822460, 45010045]}
Upvotes: 2
Reputation: 799110
Just slice-delete the values.
for v in D.itervalues():
del v[2:]
Upvotes: 0