Python - get the last element of each list in a list of lists

前端 未结 5 2175
时光取名叫无心
时光取名叫无心 2021-01-12 06:20

My question is quite simple, I have a list of lists :

my_list = [[\'a1\',\'b1\'],[\'a2\',\'b2\'],[\'a3\',\'b3\',\'c3\'],[\'a4\',\'b4\',\'c4\',\'d4\',\'e4\']]         


        
5条回答
  •  感动是毒
    2021-01-12 07:00

    You can try ,

    Here is demo.

    >>> [i.pop() for i in my_list]
    ['b1', 'b2', 'c3', 'e4']
    

    OR

    >>> my_list = [['a1','b1'],['a2','b2'],['a3','b3','c3'],['a4','b4','c4','d4','e4']]
    >>> [i[-1] for i in my_list]
    ['b1', 'b2', 'c3', 'e4']
    

提交回复
热议问题