JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Adding multiple elements to a list in Python

Author:JIYIK Last Updated:2025/05/09 Views:

List is a mutable data structure in Python. It can contain values ​​of different types.

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


append()Append a single element in a Python list using the

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

The complete sample code is as follows:

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 a new value, we will use another method to add a new value after append()the value in the list .6

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]

extend()Add multiple elements to a Python list using the

This method will extend the list by appending all the items to the iterable list. We use the append list created in the above code and append the new list elements to it.

The complete sample code is as follows:

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]

Appending multiple elements in Python list using concatenation method

+Symbol is used to concatenate and merge two lists. 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']

itertools.chainAdd multiple elements to a Python list using the

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

The complete sample code is as follows:

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']

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.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

How to Find the Maximum Value in a List in Python

Publish Date:2025/05/09 Views:99 Category:Python

This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding

Convert a list to a set in Python

Publish Date:2025/05/08 Views:145 Category:Python

This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values ​​in sequence. Python list stores comma separated values ​​between

Remove the first element from a list in Python

Publish Date:2025/05/08 Views:173 Category:Python

This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which

Convert a list to lowercase in Python

Publish Date:2025/05/08 Views:112 Category:Python

Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin

Remove all occurrences of an element from a Python list

Publish Date:2025/05/08 Views:69 Category:Python

In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element

Convert hex to bytes in Python

Publish Date:2025/05/08 Views:102 Category:Python

Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for

b in front of string in Python

Publish Date:2025/05/08 Views:54 Category:Python

This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial