Mar-03-2018, 05:23 PM
(This post was last modified: Mar-03-2018, 05:23 PM by Gribouillis.)
Here is a solution with module itertools
import itertools as it def index_two(a, b): '''return the first index where the two words differ If they don't differ, return the length of the smallest''' return next( it.dropwhile(lambda t: t[0]==t[1], zip(a, b, it.count(0))), (None, None, min(len(a), len(b))))[2] def index_many(words): '''return the first index where all the words differ identical words are considered to differ on the next character''' seq = sorted(words) if not seq: return 0 a, b = it.tee(seq) next(b, None) return max((index_two(*t) for t in zip(a, b)), default=0) def shorten_names(dic): n = index_many(dic.values()) return {k: v[:1+n] for k, v in dic.items()} dTeamNames = {'A': 'Man City', 'B': 'Man United', 'C': 'West Brom', 'D': 'West Ham'} dShortNames = shorten_names(dTeamNames) print(dShortNames)