Search code examples
pythonstringlistdictionarysublist

Most efficient way to change all items in a sublist to a string?


What's the fastest and most efficient method of applying a string function to all items in a set of sublists, when some items may be a float or integer?

For example:

List =[[1,u'6275',6275.0],[u'a',u'b',33],[u'a',44,u'c']]

I'd like to apply the str() function to all the items in the sublists to get the following

Desired_List = [['1','6275','6275'],['a','b','33'],['a','44','c']]

I'm doing this to a huge list, so I'm looking for the quickest and most efficient method. I've been looking into numpy and itertools, since they are known for their speed, but I can't figure out how to apply those. Is there a faster way at all for that matter?


Solution

  • Try:

    desired_list = [map(str, e) for e in list1]
    

    Output:

    [['6275', '6275', '6275'], ['a', 'b', 'c'], ['a', 'b', 'c']]
    

    This will be more efficient, because you don't have to load append nor call it as a function.