Create a tuple from a string and a list of strings

后端 未结 3 588
[愿得一人]
[愿得一人] 2021-01-03 23:56

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.

<
相关标签:
3条回答
  • 2021-01-04 00:32

    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.

    0 讨论(0)
  • 2021-01-04 00:39

    i think this way is better:

    my_list = my_list.insert(0,my_string)
    my_tuple = tuple(my_list)
    
    0 讨论(0)
  • 2021-01-04 00:54

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

    my_tuple = tuple([my_string] + my_list)
    
    0 讨论(0)
提交回复
热议问题