Python list of tuples, need to unpack and clean up

前端 未结 5 1879
一个人的身影
一个人的身影 2021-01-06 20:24

Assume you have a list such as

x = [(\'Edgar\',), (\'Robert\',)]

What would be the most efficient way to get to just the strings \'Edgar\

5条回答
  •  不知归路
    2021-01-06 20:39

    Here is one way:

    >>> [name for name, in x]
    ['Edgar', 'Robert']
    

    Note the placement of the comma, which unpacks the tuple.

提交回复
热议问题