I have a dictionary that contains keys and the values are a list of many ints and floats. For example:
{"key1": [6,4,3.2,0.04...], "key2": [17,0.9,50.79...]}
All the lists has the same length. I want to delete the 2nd item from each list (for example 4 in key1 and 0.9 in key2). How can I do that?
CodePudding user response:
Try this in just one line:
d = {"key1": [6, 4, 3.2, 0.04], "key2": [17, 0.9, 50.79]}
result = {k: [j for i, j in enumerate(v) if i != 1] for k, v in d.items()}
The result
will be:
{'key1': [6, 3.2, 0.04], 'key2': [17, 50.79]}
CodePudding user response:
dd = {"k1":[1,2,3,4],"k2":[11,22,33,44]}
from pprint import pp
pp({k:[v[0]] v[2:]for k,v in dd.items()}) #[v[0]] - needed because in this case returns only one element and not list
Result:
{'k1': [1, 3, 4], 'k2': [11, 33, 44]}