How to improve this code?

Hendrik van Rooyen hendrik at
Tue Sep 15 10:13:21 CEST 2009

On Tuesday 15 September 2009 03:08:59 Oltmans wrote:

> match=[1,2,3,4,5]
> def elementsPresent(aList):
> 	result=False
> 	if not aList:
> 		return False
> 	for e in aList:
> 		if e in match:
> 			result=True
> 		else:
> 			result = False
> 	return result
>  elementsPresent([6,7,8,9,5]) # should return True because 5 is
> present in list named match.

What are you trying to do?
Your code will only work if the last item in aList is in the match global.
Is this what you want?
or do you want:

(i)  a True if All the elements in match are in aList, else False?
(ii) a True if any one or more of the members of match  are in aList?
(iii)  Something else?

- Hendrik

More information about the Python-list mailing list