In this tutorial, we will learn different ways to add elements to a List in Python.

在本教程中,我们将学习使用Python向列表中添加元素的不同方法。

在Python中向List添加元素的方法 (Methods to add elements to List in Python)

There are four methods to add elements to a List in Python.

有四种方法可以在Python中将元素添加到列表中。

  1. append(): append the object to the end of the list.append():将对象追加到列表的末尾。
  2. insert(): inserts the object before the given index.insert():在给定索引之前插入对象。
  3. extend(): extends the list by appending elements from the iterable.extend():通过添加来自iterable的元素来扩展列表。
  4. List Concatenation: We can use + operator to concatenate multiple lists and create a new list.列表串联:我们可以使用+运算符来串联多个列表并创建一个新列表。

Python将元素添加到列表示例 (Python add elements to List Examples)

We can add an element to the end of the list or at any given index. There are ways to add elements from an iterable to the list. We can also use + operator to concatenate multiple lists to create a new list.

我们可以将元素添加到列表的末尾或任何给定的索引处。 有一些方法可以将元素从可迭代对象添加到列表中。 我们还可以使用+运算符连接多个列表以创建新列表。

1. append() (1. append())

This function add the element to the end of the list.

此函数将元素添加到列表的末尾。

fruits = ["Apple", "Banana"]# 1. append()
print(f'Current Fruits List {fruits}')f = input("Please enter a fruit name:\n")
fruits.append(f)print(f'Updated Fruits List {fruits}')

Output:

输出:

Current Fruits List ['Apple', 'Banana']
Please enter a fruit name:
Orange
Updated Fruits List ['Apple', 'Banana', 'Orange']

2. insert() (2. insert())

This function adds an element at the given index of the list. It’s useful to add an element at the specified index of the list.

此函数在列表的给定索引处添加一个元素。 在列表的指定索引处添加元素很有用。

num_list = [1, 2, 3, 4, 5]print(f'Current Numbers List {num_list}')num = int(input("Please enter a number to add to list:\n"))index = int(input(f'Please enter the index between 0 and {len(num_list) - 1} to add the number:\n'))num_list.insert(index, num)print(f'Updated Numbers List {num_list}')

Output:

输出:

Current Numbers List [1, 2, 3, 4, 5]
Please enter a number to add to list:
20
Please enter the index between 0 and 4 to add the number:
2
Updated Numbers List [1, 2, 20, 3, 4, 5]

3.extend() (3. extend())

This function append iterable elements to the list. It is useful to append elements from an iterable to the end of the list.

此函数将可迭代元素添加到列表中。 将元素从可迭代对象追加到列表的末尾很有用。

list_num = []
list_num.extend([1, 2])  # extending list elements
print(list_num)
list_num.extend((3, 4))  # extending tuple elements
print(list_num)
list_num.extend("ABC")  # extending string elements
print(list_num)

Output:

输出:

[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, 'A', 'B', 'C']

4.列表串联 (4. List Concatenation)

If you have to concatenate multiple lists, you can use the “+” operator. This will create a new list and the original lists will remain unchanged.

如果必须串联多个列表,则可以使用“ +”运算符。 这将创建一个新列表,而原始列表将保持不变。

evens = [2, 4, 6]
odds = [1, 3, 5]nums = odds + evens
print(nums)  # [1, 3, 5, 2, 4, 6]

The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

新列表将包含列表中从左到右的元素。 它类似于Python中的字符串连接 。

结论 (Conclusion)

It’s very easy to add elements to a List in Python programming. We can append an element at the end of the list, insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator.

在Python编程中将元素添加到列表非常容易。 我们可以在列表的末尾追加一个元素,在给定的索引处插入一个元素。 我们还可以将一个列表添加到另一个列表。 如果要串联多个列表,请使用重载的+运算符。

References:

参考文献:

  • Python ListPython清单
  • Python.org DocsPython.org文件

翻译自: https://www.journaldev.com/33182/python-add-to-list

如何在Python中将元素添加到列表相关推荐

  1. python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入

    在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...

  2. scala集合中添加元素_如何在Scala中将元素添加到列表中?

    scala集合中添加元素 In Scala, lists are immutable data structures in which adding new elements is not allow ...

  3. 如何在Python中将字典键作为列表返回?

    本文翻译自:How to return dictionary keys as a list in Python? In Python 2.7 , I could get dictionary keys ...

  4. python列表可以混合类型_如何在Python中将混合数据类型的列表转换为数据帧

    我有一个混合数据类型列表,如下所示:list = [['3D prototypes', 'Can print large objects', 'Autodesk Maya/Mudbox', '3D S ...

  5. python list增加元素_将字符串的元素添加到列表中(python)

    该程序设计用于获取由数字(任意长度)组成的字符串,并将该字符串的内容输出到列表中,一次一位数字.如果数字x小于或等于前面的数字y,则数字x将被插入到子列表中.在数字z大于y之前,x和z之间的所有内容都 ...

  6. 如何在Python中串联两个列表?

    如何在Python中串联两个列表? 例: listone = [1, 2, 3] listtwo = [4, 5, 6] 预期结果: >>> joinedlist [1, 2, 3, ...

  7. 在Python中将整数附加到列表的开头

    本文翻译自:Append integer to beginning of list in Python I have an integer and a list. 我有一个整数和一个列表. I wou ...

  8. python使用字典格式化字符串-Python中将(字典,列表等)变量格式化输出

    Python中将(字典,列表等)变量格式化成(漂亮的,树形的,带缩进的,JSON方式的)字符串输出: 变量类型是列表,列表中每个值是个字典类型变量. 格式化输出的效果,希望是那种树状结构,带缩进的,而 ...

  9. 如何在 Python 中将 Excel 文件转换为图像?Aspose快速搞定

    在各种情况下,需要将 Excel 电子表格嵌入到 Web 或桌面应用程序中.在这种情况下的解决方案之一是将 Excel 工作表转换为图像格式.在本文中,将学习如何在 Python中将Excel XLS ...

最新文章

  1. CISCO设备部分型号IOS下载
  2. getopt:命令行选项、参数处理
  3. 智能驾驶继续突破,国内国外技术进入深水区
  4. Powershell AWS 自动化管理 (6) - IAM
  5. C++11 - sizeof用于类成员
  6. Faster RCNN 训练自己的检测模型
  7. TCP/IP:IP多播选路
  8. Kafka从上手到实践 - 庖丁解牛:Topic Broker | 凌云时刻
  9. CentOS 6 和CentOS 7 的区别
  10. Java构造方法解析
  11. Multisim14.0安装教程
  12. 四叉树——图片应用实例
  13. 数学问题-标量三重积向量三重积
  14. 美国梅西学院计算机科学与技术,新西兰梅西大学计算机科学硕士专业很难吗?看完入学条件就知道了...
  15. 001-三阶魔方-概述及层先法
  16. Java根据图片生成GIF动图
  17. uni-app介绍及创建
  18. 从NVIDIA官方网站上下载CUDA的方法
  19. 盗号、薅羊毛、机器注册、恶意爬虫——618大促背后涌动的欺诈暗流
  20. 如何计算满足指定条件下数值的平均值——DAVERAGE函数的使用

热门文章

  1. 20145202马超《网络对抗》Exp8 Web基础
  2. inner join ,left join ,right join 以及java时间转换
  3. 支持iCloud简记
  4. 如何部署windows服务?
  5. android_Media
  6. [转载] Python中的string模块的学习
  7. 设计方案--移动端延迟300ms的原因以及解决方案
  8. CentOS 7 配置网络连接
  9. 【图像处理】openCV库教程
  10. 解析C#中[],List,Array,ArrayList的区别及应用