Skip to content Skip to sidebar Skip to footer

Create A Tuple From A String And A List Of Strings

I need to combine a string along with a list of strings into a tuple so I can use it as a dictionary key. This is going to be in an inner loop so speed is important. The list will

Solution 1:

I can't speak for performance, but this is definitely the simplest I can think of:

my_tuple = tuple([my_string] + my_list)

Solution 2:

The straightforward way is simply my_tuple = tuple( my_list + [my_string] ). I would certainly start with that and see if the performance is acceptable before trying to figure out any crazy ways of subverting the normal system for speed.

Solution 3:

i think this way is better:

my_list = my_list.insert(0,my_string)
my_tuple = tuple(my_list)

Post a Comment for "Create A Tuple From A String And A List Of Strings"