更优雅/更短的方式有条件地将键值对添加到字典?

时间:2015-08-28 14:09:29

标签: python dictionary key-value

是否有更优雅/更短的方式有条件地将键值对添加到python中的字典而不是:

extra = {'foo': 'bar'}
if something is True:
   extra['some'] = 'thing'

1 个答案:

答案 0 :(得分:-1)

如何使用update()

extra = {'foo': 'bar'} extra.update({'some': 'thing'})

相关问题