Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print items in deque (python)

I have this code:

import collections

def last3scores():
    return collections.deque([], 3)

user_last3 = collections.defaultdict(last3scores)

#after this I have some more code and then this:

user_last3[name].append(score)

print(str(user_last3))

But when I run the program, I get this:

defaultdict(<function last3scores at 0x0000000003806E18>, {'nick': deque([2], maxlen=3)})

What I'd like to get is this:

{'nick': [2]}

Is there a way to accomplish that in Python 3.* ?

like image 740
Nick Avatar asked Jun 04 '26 21:06

Nick


1 Answers

This should do the trick (in Python 3.* switch to items instead of iteritems):

>>> {k:list(v) for k,v in user_last3.iteritems()}
{'nick': [2]}
like image 149
Idos Avatar answered Jun 06 '26 10:06

Idos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!