Pair strings in list based on containing text in Python
Question: I'm looking to take a list of strings and create a list of tuples that groups
items based on whether they contain the same text.

For example, say I have the following list:

    
    
    MyList=['Apple1','Pear1','Apple3','Pear2']
    

I want to pair them based on all but the last character of their string, so
that I would get:

    
    
    ListIWant=[('Apple1','Apple3'),('Pear1','Pear2')]    
    

We can assume that only the last character of the string is used to identify.
Meaning I'm looking to group the strings by the following unique values:

    
    
    >>> list(set([x[:-1] for x in MyList]))
    ['Pear', 'Apple']
    


Answer: 
    In [69]: from itertools import groupby
    
    In [70]: MyList=['Apple1','Pear1','Apple3','Pear2']
    
    In [71]: [tuple(v) for k, v in groupby(sorted(MyList, key=lambda x: x[:-1]), lambda x: x[:-1])]
    Out[71]: [('Apple1', 'Apple3'), ('Pear1', 'Pear2')]
    

