天天看點

如何在Python中将元素添加到清單

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 List

    Python清單

  • Python.org Docs

    Python.org檔案

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