Multiple different ways of doing something? We'd better... run speed tests!!!!
...and something's fishy about the set speed... I was expecting that to do better. (that said, they're all close enough that it doesn't matter)
...and something's fishy about the set speed... I was expecting that to do better. (that said, they're all close enough that it doesn't matter)
Output:D:\Projects\playground>python -VV
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)]
D:\Projects\playground>python temp.py
original: 6.258850955969614
nilamo: 4.656287876513283
buran: 4.732732260557226
volcano63: 5.075134360387665
D:\Projects\playground>python temp.py
original: 6.198476880215798
nilamo: 4.8221962886350305
buran: 4.808510175991959
volcano63: 5.1591497014125824
D:\Projects\playground>python temp.py
original: 6.5075879446218705
nilamo: 4.677959760624388
buran: 4.862944503352491
volcano63: 5.2680807758374115
Testing code:def original(): word = "abcde" twoletter = [] for x in range(0, (len(word)-1)): a = word[x] b = word[x+1] c = a + b d = b + a twoletter.append(c) twoletter.append(d) if x + 2 < len(word): a = word[x] b = word[x + 2] c = a + b d = b + a twoletter.append(c) twoletter.append(d) if x + 3 < len(word): a = word[x] b = word[x + 3] c = a + b d = b + a twoletter.append(c) twoletter.append(d) if x + 4 < len(word): a = word[x] b = word[x + 4] c = a + b d = b + a twoletter.append(c) twoletter.append(d) return twoletter def nilamo(): word = "abcde" twoletter = [] for left in word: for right in word: if left != right: twoletter.append(left + right) return twoletter import itertools def buran(): word = "abcde" # using list comprehension my_list = [''.join(perm) for perm in itertools.permutations(word,2)] return my_list def volcano63(): word_set = set('abcde') two_letter = [left + right for right in word_set for left in word_set - set([right])] return two_letter if __name__ == "__main__": import timeit for func in ["original", "nilamo", "buran", "volcano63"]: print("{0}: {1}".format(func, timeit.timeit("{0}()".format(func), globals=globals())))