How can you simplify the expression?

policy = [] for i in res2: policy.append(tuple(i)) print policy 

    2 answers 2

     policy = [tuple(o) for o in res2] 
    • You can also remove the call to the tuple function by replacing it with a construct using a literal: policy = [(o, ) for o in res2] - Timofei Bondarev
    • one
      But this is already a controversial assumption, since the question about the nature of the res2 elements does not say anything, and (o, ) may lead to a different result - Kozlov Sergei
    • Indeed, if it is iterable, the behavior will change. Hurry up. - Timofei Bondarev

    map(tuple, res2)

    • 2
      It is important to note that the list when executing the expression will be received only in python 2, in python 3 for this effect it is necessary to call the list function: policy = list(map(tuple, res2)) - Timofei Bondarev
    • According to Guido, list comprehension ([f (i) for i in a]) is preferable to use than map. He even wanted to remove the map and filter from python3: artima.com/weblogs/viewpost.jsp?thread=98196 - dzhioev