[Tutor] checking if all items in a list are the same

Deirdre Saoirse Moen deirdre@deirdre.net
Mon, 7 May 2001 15:27:28 -0700


>If I have a list, how can I compare all of the elements to check if 
>they are the same?  Say that I have the following list:
>
>[e,e,e,e,e]
>
>How can I tell the computer to go item by item in the list and 
>compare them.  If they are the same, I want the computer to tell me 
>that it has found the identity element, and if they are not the 
>same, I want the computer to tell me that there is no identity 
>element. Can anyone help?


If you return a -1 from a function when there's no match, or a zero 
when there's a match.

This assumes ALL items in the list are identical to the match item:


def listMatch(aList, itemToMatch)

	for i in aList:
		if i != itemToMatch:
			return -1
	return 0


-- 
_Deirdre     Stash-o-Matic: http://weirdre.com      http://deirdre.net
"I love deadlines. I like the whooshing sound they make as they fly by."
                                                          - Douglas Adams