Python dict del
2016-04-13 22:43
856 查看
Best
way to remove an item from a Python dictionary?
up vote56down votefavorite 13 | What is the best way to remove an item from a dictionary? Here's a simple approach:for key, item in some_dict.items(): if item is item_to_remove: del some_dict[key] Are there better ways? Is there anything wrong with mutating (deleting items) from the dictionary while iterating it? python
| ||||
a comment |
9 Answers
activeoldestvotesup vote52down voteaccepted | Be aware that you're currently testing for object identity (isonly returns Trueif both operands are represented by the same object in memory - this is not always the case with two object that compare equal with ==). If you are doing this on purpose, then you could rewrite your code as some_dict = {key: value for key, value in some_dict.items() if value is not value_to_remove} But this may not do what you want: >>> some_dict = {1: "Hello", 2: "Goodbye", 3: "You say yes", 4: "I say no"} >>> value_to_remove = "You say yes" >>> some_dict = {key: value for key, value in some_dict.items() if value is not value_to_remove} >>> some_dict {1: 'Hello', 2: 'Goodbye', 3: 'You say yes', 4: 'I say no'} >>> some_dict = {key: value for key, value in some_dict.items() if value != value_to_remove} >>> some_dict {1: 'Hello', 2: 'Goodbye', 4: 'I say no'} So you probably want !=instead of is not.
| ||||||||||||||||||||
a comment |
up vote64down vote | >>> dic = {'a':1, 'b':2} >>> dic {'a': 1, 'b': 2} >>> dic.pop('c', 0) 0 >>> dic.pop('a', 0) 1 >>> dic {'b': 2}
| ||
add a comment |
up vote28down vote | a = {'name': 'your_name','class': 4} if 'name' in a: del a['name']
| |||
add a comment |
up vote23down vote | A simple comparison between del and pop():import timeit code = """ results = {'A': 1, 'B': 2, 'C': 3} del results['A'] del results['B'] """ print timeit.timeit(code, number=100000) code = """ results = {'A': 1, 'B': 2, 'C': 3} results.pop('A') results.pop('B') """ print timeit.timeit(code, number=100000) result: 0.0329667857143 0.0451040902256 So, del is faster than pop().
| ||||||||
a comment |
up vote7down vote | items()returns a list, and it is that list you are iterating, so mutating the dict in the loop doesn't matter here. If you were using iteritems()instead, mutating the dict in the loop would be problematic, and likewise for viewitems()in Python 2.7. I can't think of a better way to remove items from a dict by value.
| ||
add a comment |
up vote5down vote | I'd build a list of keys that need removing, then remove them. It's simple, efficient and avoids any problem about simultaneously iterating over and mutating the dict.keys_to_remove = [key for key, value in some_dict.iteritems() if value == value_to_remove] for key in keys_to_remove: del some_dict[key]
| ||
add a comment |
up vote0down vote | There is nothing wrong with deleting items from the dictionary while iterating, as you've proposed. Be careful about multiple threads using the same dictionary at the same time, which may result in a KeyError or other problems. Of course, see the docs at http://docs.python.org/library/stdtypes.html#typesmapping |
相关文章推荐
- 9.Python笔记之面向对象高级部分
- Python 中enumerate的使用
- Python你必须知道的十个库
- Python轻量级库web.py
- python 简单爬虫原理
- Python装饰器
- 数字:
- pyspark使用 jupyter ,matplotlib, ipython
- 初识Python
- ubuntu安装pip+python27+ipython+scrapy+zlib-及遇到的各种问题解决
- ubuntu-pip+python27+ipython-安装-及遇到的各种问题解决
- python基本数据类型-字典
- sublime 安装Python
- python基本数据类型-元组
- Python3爬虫实战:爬取大众点评网某地区所有酒店相关信息
- python的函数
- 使用2to3将代码移植到Python 3
- Python模块受欢迎排行榜Top200
- python文件操作
- Windows下Python包的pip安装