how can i slice values in ordereddict in python?

@Caina is close, but his version is not quite right in that it leaves an extra collection layer in the result. This is the expression that returns the exact result you requested:

x_sliced = OrderedDict({k:x[k][0] for k in x})


OrderedDict([(40, array([1, 2]))])

Actually, this isn’t technically what you asked for. Your version has one missing closing ‘)’, but that’s just a typo I assume.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top