How to add an element to the front of a list in Python
This tutorial explains how to insert an element to the front of a list in Python. This tutorial also lists some sample codes to help you understand.
insert()
Use the method to insert an element to the front of a list
in Python
insert()
One of the most commonly used methods is
using . It is provided insert()
by list
the library. list.insert(pos, element)
It takes two parameters, pos
and element
as parameters. pos
It defines the position of the element.
The following is sample code using this method.
lists = ["james", "tim", "jin"]
lists.insert(0, "steve")
print(lists)
Output:
['steve', 'james', 'tim', 'jin']
However, list.insert()
the operation consumes a lot of time. To improve the time performance, we can use collections.deque
the method.
deque.appendleft()
Use the method to insert an element to the front of a list
in Python
Python's collections
add_on module provides a variety of functions. In Python 2.4, collections
add_on was added deque()
, a double-ended queue. It is a list-like container that is efficient in appending and popping. deque
The add_on function has a single appendleft(element)
method. It accepts an element and appends it to the beginning of the list.
The sample code for this method is given below.
import collections
dequeue = collections.deque([5, 2, 6, 8, 1])
print(dequeue)
dequeue.appendleft(10)
print(dequeue)
Output:
deque([5, 2, 6, 8, 1])
deque([10, 5, 2, 6, 8, 1])
Create a new list in Python and append it to the list
A very simple and trivial solution is to create a new list with the desired element x at index 0 of the list. Of course, you don't prepend x to the list, but create a new list with x already at the first position of the list.
The basic code for this approach is given below.
lists = ["james", "tim", "jin"]
new_list = ["x"] + lists
print(new_list)
Output:
['x', 'james', 'tim', 'jin']
Use list slicing in Python to insert elements to the front of a list
List slicing is another way to prepend elements to a list. An element is prepended to the list by assigning the 0th element to it.
The sample code of this method is as follows.
temp_list = [4, 5, 8, 10, 13]
print(temp_list)
temp_list[:0] = [12]
print(temp_list)
Output:
[4, 5, 8, 10, 13]
[12, 4, 5, 8, 10, 13]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
在 Python 中将 Pandas 系列的日期时间转换为字符串
Publish Date:2024/04/24 Views:938 Category:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
Publish Date:2024/04/24 Views:1165 Category:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Python 中将 Tensor 转换为 NumPy 数组
Publish Date:2024/03/12 Views:136 Category:Python
-
在 Python 中,可以使用 3 种主要方法将 Tensor 转换为 NumPy 数组:Tensor.numpy()函数,Tensor.eval()函数和 TensorFlow.Session()函数。
在 Python 中将 NumPy 数组保存为图像
Publish Date:2024/03/12 Views:160 Category:Python
-
本教程演示了如何将 numpy 数组另存为 python 中的图像
在 Python 中将 CSV 读取为 NumPy 数组
Publish Date:2024/03/12 Views:143 Category:Python
-
本教程演示如何在 Python 中将 CSV 读取为 NumPy 数组。
在 Python 中将 3D 数组转换为 2D 数组
Publish Date:2024/03/12 Views:164 Category:Python
-
NumPy 包的 reshape()函数可用于在 Python 中将 3D 数组转换为 2D 数组。
在 Python 中将 NumPy 数组写入 CSV
Publish Date:2024/03/12 Views:177 Category:Python
-
本教程演示如何将 numpy 数组保存在 csv 文件中
在 Python 的 numpy.exp()函数中遇到的溢出错误
Publish Date:2024/03/12 Views:230 Category:Python
-
了解如何在 Python 中处理 numpy.exp() 函数中的溢出错误