我有这个清单:
[('1', '1')]
我想在列表前添加一个dict对象,看起来像:
[('All', 'All'), ('1', '1')]
我正在努力:
myList[:0] = dict({'All': 'All'})
但这给了我:
['All', ('1', '1')]
我究竟做错了什么?
使用items()
字典获取键,值并将它们放在列表的前面:
lst = [('1', '1')]
lst = list({'All': 'All'}.items()) + lst
print(lst)
# [('All', 'All'), ('1', '1')]
注意:{'All': 'All'}
本身就是字典,因此dict({'All': 'All'})
在您的代码中是不必要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句