在Python列表的第一位置插入[关闭]


131

如何在列表的第一个索引处插入元素?如果我使用list.insert(0,elem),elem是否会修改第一个索引的内容?还是我必须使用第一个元素创建一个新列表,然后将旧列表复制到这个新列表中?

Answers:


245

用途insert

In [1]: ls = [1,2,3]

In [2]: ls.insert(0, "new")

In [3]: ls
Out[3]: ['new', 1, 2, 3]

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.