Hướng dẫn append multiple elements python

You can use the sequence method list.extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.

>>> lst = [1, 2] >>> lst.append(3) >>> lst.append(4) >>> lst [1, 2, 3, 4] >>> lst.extend([5, 6, 7]) >>> lst.extend((8, 9, 10)) >>> lst [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> lst.extend(range(11, 14)) >>> lst [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

So you can use list.append() to append a single value, and list.extend() to append multiple values.

In this article given a set and list of elements, the task is to write a Python program to append multiple elements in set at once.

Example:

Input : test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 10]

Output : {1, 2, 4, 5, 6, 7, 9, 10}

Explanation : All elements are updated and reordered. (5 at 3rd position).

Input : test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 8]

Output : {1, 2, 4, 5, 6, 7, 8, 9, 10}

Explanation : All elements are updated and reordered. (8 at 7th position).

Method #1 : Using update()

In this, we use in built update() to get all the elements in list aligned with the existing set.

Python3

test_set = {6, 4, 2, 7, 9}

print("The original set is : " + str(test_set))

up_ele = [1, 5, 10]

test_set.update(up_ele)

print("Set after adding elements : " + str(test_set))

Output:

The original set is : {2, 4, 6, 7, 9} Set after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}

Method #2 : Using | operator ( Pipe operator )

The pipe operator internally calls union(), which can be used to perform task of updating set with newer elements. 

Python3

test_set = {6, 4, 2, 7, 9}

print("The original set is : " + str(test_set))

up_ele = [1, 5, 10]

test_set |= set(up_ele)

print("Set after adding elements : " + str(test_set))

Output:

The original set is : {2, 4, 6, 7, 9} Set after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}

  1. HowTo
  2. Python How-To's
  3. Append Multiple Elements to List in Python

Created: February-20, 2021 | Updated: November-26, 2021

  1. Append Single Element in the Python List Using the append() Function
  2. Append Multiple Elements in the Python List Using the extend() Function
  3. Append Multiple Elements in the Python List Using the Concatenation Method
  4. Append Multiple Elements in the Python List Using the itertools.chain Function

The list is a mutable data structure in Python. It could contain different types of values.

This article will discuss some methods to append single or multiple elements in a Python list.

Append Single Element in the Python List Using the append() Function

The append() method adds a single value to the end of the list.

The complete example code is given below:

lst=[2,4,6,'python'] lst.append(6) print("The appended list is:",lst)

Output:

The appended list is: [2, 4, 6, 'python', 6]

Similarly, to add one more new value, we will use another append() method to add another new value after the value 6 in the list.

lst=[2,4,6,'python'] lst.append(6) lst.append(7) print("The appended list is:",lst)

Output:

The appended list is: [2, 4, 6, 'python', 6, 7]

Append Multiple Elements in the Python List Using the extend() Function

This method will extend the list by adding all items to the iterable. We use the appended list as created in the above code and add the new list elements into it.

The complete example code is given below:

lst=[2,4,6,'python'] lst.extend([8,9,10]) print("The appended list is:",lst)

Output:

The appended list is: [2, 4, 6, 'python', 8, 9, 10]

Append Multiple Elements in the Python List Using the Concatenation Method

The + symbol is used for concatenation and merges two list. The complete example code is given below:

lst1=[2,4,6,8] lst2=['python','java'] lst3=lst1+lst2 print("The Concatenated List is:",lst3)

Output:

The Concatenated List is: [2, 4, 6, 8, 'python', 'java']

The chain() function is imported from the itertools. The purpose of the chain function is the same as the concatenation operator +. It will combine all the list’s elements into a new list. The performance of this method is much efficient than other methods.

The complete example code is given below:

from itertools import chain lst1=[2,4,6,8] lst2=['python','java'] final_list=list(chain(lst1,lst2)) print("The Final List is:",final_list)

Output:

The Final List is: [2, 4, 6, 8, 'python', 'java']

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • Chủ đề