import itertools


def get_spells(sequence):
	"""
	Returns a list of tuples where each tuple holds the element and the length of the spell (also known as run or episode) for each spell in the sequence.
	
	Example
	---------
	>>> sequence = [1,1,2,1,2,2,3]
	>>> ps.get_spells(sequence)
	[(1, 2), (2, 1), (1, 1), (2, 2), (3, 1)]
	"""
	
	# get each spell and its length
	spells = [(k, sum(1 for x in v)) for k,v in itertools.groupby(sequence)]
	# this is functionally equivalent to the following;
	# spells = [(k, len(list(v))) for k,v in itertools.groupby(sequence)]
	
	return spells