Python3 function that adds elements from master list together

Viewed 22

So if master list is: master_list = [“tom”, “bob”]

The new list should be: new_list = [‘tomtom”, “tombob”, “bobbob”, “bobtom”]

1 Answers

You can use itertools.product to generate the desired strings:

import itertools

master_list = ["tom", "bob"]
new_list = list(''.join(entry) for entry in itertools.product(master_list, repeat=2))

# Prints ['tomtom', 'tombob', 'bobtom', 'bobbob']
print(new_list)
Related