Search code examples
pythondjangocombinationspython-itertools

Removing Brackets from itertools combination list results


I have this code

import itertools
import random

def gen_reset_key():
    key = random.randint(100, 199)
    return key

key_combinations = list(itertools.combinations(str(gen_reset_key()), 2))

key_one = key_combinations[0]
key_two = key_combinations[1]
key_three = key_combinations[2]

print("Key one is: {}".format(key_one))
print("Key two is: {}".format(key_two))
print("Key three is: {}".format(key_three))

The output is:

Key one is: ('1', '9')
Key two is: ('1', '9')
Key three is: ('9', '9')

I want to remove the brackets and speech marks from the output. How can I achieve that?

so that my output would be:

Key one is 19
Key two is 19
Key three is 99

Solution

  • You can accomplish this in the following way (without using str.join):

    print('Key one is: {}{}'.format(*key_one))
    print('Key two is: {}{}'.format(*key_two))
    print('Key three is: {}{}'.format(*key_three))
    

    This should have the added benefit of avoiding the creation of intermediate strings in the join.