Deleting a few list items inside of dictionary

前端 未结 5 1960
余生分开走
余生分开走 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:43

    http://docs.python.org/2/tutorial/datastructures.html#the-del-statement del is a way to delete by index not value, however you can search the index first if you want. Better to use remove if you want to delete by value.

    To delete the element at index 1 and 2

    >>> phone = {"first":100,"second":200,"third":[10,12,5,38],"fourth":400}
    >>> del phone["third"][1]
    >>> del phone["third"][2]
    >>> phone
    {'second': 200, 'fourth': 400, 'third': [10, 5], 'first': 100}
    

提交回复
热议问题