0

I tried searching for this an answer to this question for a long time now. I tried everywhere, but could be that I am searching with the wrong keywords, if so, please forgive me for asking a stupid or already answered question.

I am trying to pop a dictionary in python 2.2. The following is a snippet of my code that I am trying to run:

ABRA= {}
ABRA[0] = ['MENU', ['TV', 'MENU']]
ABRA[1] = ['TV', 'PC', ['RM', 'LM']]
count = 0
KADABRA = ABRA.pop(count).pop()
print(str(KADABRA))
print(len(KADABRA))
count += 1
KADABRA = ABRA.pop(count)
print(str(KADABRA))

When I enter this code in an online interpreter like Codepad, it works and I get the desired output, when I run it on the server where I would like it to run, it doesn't. I get the following error:

AttributeError ('dict' object has no attribute 'pop').

I don't see a mistake in the code, or in the way that I am calling the pop. I even tried to remove the 'double' pop. Still an error. If I just make it a list instead of a dict like this:

ABRA = ['MENU', ['TV', 'MENU']]
KADABRA = ABRA.pop()
print(str(KADABRA))
print(len(KADABRA))

Then it works and I get the right prints. But I don't want a list of lists, but a dict. I have seen examples of popping with dicts. So my question is why can't i pop the dict on my server and/or is there an alternative to popping with dicts?

Anto
  • 6,806
  • 8
  • 43
  • 65

1 Answers1

2

From the dict.pop() documentation:

New in version 2.3.

In other words, there is no such method in Python 2.2.

You can implement this yourself with:

_sentinel = object()

def pop_dict(d, k, default=_sentinel):
    try:
        v = d[k]
        del d[k]
        return v
    except KeyError:
        if default is _sentinel:
            raise
        return default

Use this as:

pod_dict(ABRA, count)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you for responding, but as a question to follow this up: my server also denies the use of try except, is there an elegant solution to check if the key is not in the dict? – Martijn Claes Sep 01 '14 at 11:53
  • @MartijnClaes: then ask for permission rather than for forgiveness: `if k in d:`. – Martijn Pieters Sep 01 '14 at 11:55
  • @MartijnClaes: what strange server is this that denies using `try...except` however? – Martijn Pieters Sep 01 '14 at 11:56
  • I know.. I've never come across something like that as well. I am a student doing my internship at this firm and the server they use is outdated and boarded shut. It is another program that executes the .py scripts and that program seems to block everything. – Martijn Claes Sep 01 '14 at 12:11
  • @MartijnClaes: is this server Zope perhaps? – Martijn Pieters Sep 01 '14 at 12:14
  • Don't really know Zope, but from what I read (Wikipedia), I think it is not Zope. Since it isn't web based. It is just a Win XP machine which runs tasks that consist of the .py scripts. Software that runs the tasks is third party. – Martijn Claes Sep 01 '14 at 12:27
  • @MartijnClaes: yeah, my guess wouldn't even apply here as the Python Script support in Zope allows for `try..except`. They must be using a different form of the [restricted Python execution environment](https://pypi.python.org/pypi/RestrictedPython) than what we used. – Martijn Pieters Sep 01 '14 at 12:28