Deleting a few list items inside of dictionary

前端 未结 5 1935
余生分开走
余生分开走 2021-01-27 13:55

Deleting a few list items inside of dictionary

Hi, I have a dictionary:

phone = {\"first\":100,\"second\":200,\"third\":[10,12,5,38],\"fourth\":400}
         


        
5条回答
  •  离开以前
    2021-01-27 14:41

    You can use del() or you can re-create the list by filtering it:

    >>> phone = {"first":100,"second":200,"third":[10,12,5,38],"fourth":400}
    >>> phone['third'] = [x for x in phone['third'] if x not in (12,5)]
    >>> phone
    {'second': 200, 'fourth': 400, 'third': [10, 38], 'first': 100}
    

提交回复
热议问题