For triangles:
You can use the built-in all to sort the list of contents first, as their order may differ from what is generated from itertools.combinations :
sLines = [tuple(sorted(l)) for l in Lines] dataE = itertools.combinations('BCD', 2)
Now you can call all , which will check that every value in dataE present in sLines :
all(l1 in sLines for l1 in dataE)
which will return True .
So your check_geometry function might look something like this:
def check_geometry(line, geometry): sLines = [tuple(sorted(l)) for l in line] dataE = itertools.combinations(geometry, 2) return all(l1 in sLines for l1 in dataE)
Now the calls made will check if the Lines geometry contains:
check_geometry(Lines, 'BCD')
A bit more general:
To summarize this a bit, we can opt out of itertools.combinations and use zip instead. The following makes some relevant changes to the function in order to put a zip , but does similar things:
def check_geometry(line, geometry): sLines = [sorted(l) for l in line] dataE = [sorted(x) for x in zip(geometry, geometry[1:] + geometry[:1])] return all(l1 in sLines for l1 in dataE)
The key difference here is:
dataE now presents a list of lists containing the result of zip(geometry, geometry[1:] + geometry[:1]) . What zip does in this case is that it takes the line as "BCDA" and the same line with the first element added to the end of geometry[1:] + geometry[:1] (ie "CDAB" ), and creates records indicating the sides of the form:
>>> s = "BCDA" >>> s[1:] + s[:1] >>> 'CDAB' >>> list(zip(s, s[1:] + s[:1])) [('B', 'C'), ('C', 'D'), ('D', 'A'), ('A', 'B')]
Now we can verify that the geometry with the "BCDA" points can be built with lines in Lines :
check_geometry(Lines, "BCD") # True check_geometry(Lines, "BCDA") # True check_geometry(Lines, "BCDF") # False
Note 1 : Lines can be written as:
Lines=[('B', 'C'), ('D', 'A'), ('D', 'C'), ('A', 'B'), ('D', 'B')]
In brackets () and a comma , there is no additional effect, you can leave them :-).
Note 2 The geometry parameter for check_geometry can be any iterable (tuples, lists, strings):
check_geometry(lines, "BCD") == check_geometry(lines, ('B', 'C', 'D'))
Creating and tuple in this case seems somewhat strange (alas, you may have good reason for this). If the reason does not require this, I would suggest going with the strings as the value of the geometry parameter.