A merged containers class
Definition
from itertools import chain
class merged (object):
"""
Shadows multiple containers, treating them as one, performing
iteration and membership testing over all of them in the order the
class is instantiated with.
"""
def __init__ (self, *args):
self._containers = args
def __len__ (self):
return sum(len(c) for c in self._containers)
def __iter__ (self):
return chain(*self._containers)
def __contains__ (self, item):
for container in self._containers:
if item in container:
return True
return False
(source)
Usage
letters = "a b c".split()
numbers = set((1, 2, 3))
both = merged(letters, numbers)
for item in both:
print item,
# prints a b c 1 2 3
"a" in both # True
"e" in both # False
2 in both # True
8 in both # False