views:

87

answers:

3

I retrieved data from the log table in my database. Then I started finding unique users, comparing/sorting lists, etc.

In the end I got down to this.

stats = {'2010-03-19': {'date': '2010-03-19', 'unique_users': 312, 'queries': 1465}, '2010-03-18': {'date': '2010-03-18', 'unique_users': 329, 'queries': 1659}, '2010-03-17': {'date': '2010-03-17', 'unique_users': 379, 'queries': 1845}, '2010-03-16': {'date': '2010-03-16', 'unique_users': 434, 'queries': 2336}, '2010-03-15': {'date': '2010-03-15', 'unique_users': 390, 'queries': 2138}, '2010-03-14': {'date': '2010-03-14', 'unique_users': 460, 'queries': 2221}, '2010-03-13': {'date': '2010-03-13', 'unique_users': 507, 'queries': 2242}, '2010-03-12': {'date': '2010-03-12', 'unique_users': 629, 'queries': 3523}, '2010-03-11': {'date': '2010-03-11', 'unique_users': 811, 'queries': 4274}, '2010-03-10': {'date': '2010-03-10', 'unique_users': 171, 'queries': 1297}, '2010-03-26': {'date': '2010-03-26', 'unique_users': 299, 'queries': 1617}, '2010-03-27': {'date': '2010-03-27', 'unique_users': 323, 'queries': 1310}, '2010-03-24': {'date': '2010-03-24', 'unique_users': 352, 'queries': 2112}, '2010-03-25': {'date': '2010-03-25', 'unique_users': 330, 'queries': 1290}, '2010-03-22': {'date': '2010-03-22', 'unique_users': 329, 'queries': 1798}, '2010-03-23': {'date': '2010-03-23', 'unique_users': 329, 'queries': 1857}, '2010-03-20': {'date': '2010-03-20', 'unique_users': 368, 'queries': 1693}, '2010-03-21': {'date': '2010-03-21', 'unique_users': 329, 'queries': 1511}, '2010-03-29': {'date': '2010-03-29', 'unique_users': 325, 'queries': 1718}, '2010-03-28': {'date': '2010-03-28', 'unique_users': 340, 'queries': 1815}, '2010-03-30': {'date': '2010-03-30', 'unique_users': 329, 'queries': 1891}}

It's not a big dictionary. But when I try to do one last thing...it craps out on me.

 for k, v in stats:
    mylist.append(v)

too many values to unpack

What the heck does that mean??? TOO MANY VALUES TO UNPACK.

+7  A: 

If you only want the values you could just do:

mylist = stats.values()

If you need the key - value pair you should iterate the dict's items:

mylist = []
for k,v in stats.iteritems():
    mylist.append(v)

In the code in your question you are just iterating over the dicts keys.

Since you assign a single string (the key) to the tuple (k,v) it's characters are instead iterated; it's logical that this complete string cannot be unpacked' into a (k,v) tuple unless in a situation where you would have a string with a length of exactly two. That explains the error message: you're trying to unpack every single character of the string into only two placeholders (k and v).

ChristopheD
just nitpicking, but as @Dave Kirby points out they keys are actually being unpacked, as strings character by character and hence too _many_ values to unpack
Davy8
If you actually do want to iterate over both the keys and values, I would suggest using `stats.iteritems()` instead of `stats.items()` because it doesn't create a whole new list of the dictionary's contents.
David Zaslavsky
@Davy8: yeah, updated my answer a little to make it a bit more clear...
ChristopheD
@David: yes, that would be a good fit for this situation.
ChristopheD
+5  A: 

The problem is that when you iterate through a dictionary using a for ... in ... loop you only iterate through the keys. You can demonstrate this with:

>>> for x in stats: print x
... 
2010-03-19
2010-03-18
etc

so when you do for k,v in stats: it is trying to assign a string to the tuple (k,v). Since strings are iterable it will try to assign a character at a time, so it would assign '2' to k and '0' to v, but has nowhere to assign the remaining characters to - hence the error.

What you want is to either call the dict iteritems method to get a sequence of (key, value) tuples, or to call the itervalues method to get a sequence of the values since you are not using the key.

Dave Kirby
+1  A: 

The default behavior when iterating a dictionary is equivalent to calling its keys() method.

So the following are equivalent:

>>> for k in stats: print k
>>> for k in stats.keys(): print k

If you want to iterate over keys and values in a single run, you must call items().

jathanism