My first objective is to make a table that counts the instances of a particular number of byte allocations. In other words, my desired output for the above input would be something like:

48 bytes -> 3 times
96 bytes -> 1 times
24 bytes -> 2 times

(for now, I'm not concerned about the memory addresses)

Since I'm using Python, I thought doing this using a dictionary would be the right way to go (based on about 3 hours' worth of reading Python tutorials). Is that a good idea?

In trying to do this using a dictionary, I decided to make the number of bytes the 'key', and a counter as the 'value'. My plan was to increment the counter on every occurrence of the key. As of now, my code snippet is as follows:

With this, I get a syntax error in the 'has_key' call, which leads me to question whether it is even possible to use variables as dictionary keys. All examples I have seen so far assume that keys are available upfront. In my case, I can get my keys only when I'm parsing the input file.

(Note that my input file can run into thousands of lines, with hundreds of different keys)

+1: If you are only interested in the count, Counter is the way to go. On the other hand, the OP wrote: for now, I'm not concerned about the memory addresses --- I suppose he might sooner or later need a custom solution that goes beyond Counter.
–
Ferdinand BeyerNov 28 '11 at 10:19

Thank you very much for this solution. I tried it, but it didn't work. This is because Counter is available only for Python > 2.7, and I'm using 2.6.4. But it led me to: stackoverflow.com/questions/3594514/…, and here I found a way to solve my problem. But I'm marking this answer as the solution, because this is probably the best way of solving the problem.
–
GautamNov 28 '11 at 10:37

You most definitely can use variables as dict keys. However, you have a variable called numBytes, but are using a string containing the text "numBytes" - you're using a string constant, not the variable. That won't cause the error, but is a problem. Instead, try:

if numBytes in allocationList:
# do stuff

Additionally, consider a Counter. This is a convenient class for handling the case you're looking at.