Salihin Alim
Salihin Alim

Reputation: 11

Set function in Python 2.2

I have a problem regarding set in Python 2.2. Currently I am just comparing between two lists like so:

temp3 = set(list1) - set(list2)

But it keeps prompting me that set isn't define. I used 2.7 to create the program. Updating software is not an option sadly. Is there an alternative I can use?

Upvotes: 1

Views: 689

Answers (2)

John Y
John Y

Reputation: 14519

You can try third-party modules which provide the missing set functionality. For example, demset.

The simplest way to use this module is to keep it in the same directory as the program you are writing and import the desired contents like so:

from demset import set

The documentation as well as home page mention a way to use Python's built-in set (and frozenset) when available, and only use the versions in the demset module when the built-ins are not available:

try:
    type(frozenset)
except NameError:
    from demset import set, frozenset

Aside from those imports, your program can stay exactly the same.

Note that I mentioned keeping the demset module in the same directory as your program only because this doesn't require any installation, and if you are stuck on Python 2.2, it sounds like maybe you are not allowed to install anything.

Upvotes: 1

aw4lly
aw4lly

Reputation: 2155

Unless you're doing something massive you can probably just write your own function for this like the one below.

tmp3=[]
for i in list1:
  if i not in list2 and i not in tmp3:
    tmp3.append(i)

Upvotes: 0

Related Questions