Python add list values to list

Python Append List to a List Example

How to append a list to another list in Python? We are often required to append a list to another list to create a nested list. Python provides an append() method to append a list as an element to another list. In case you wanted to append elements from one list to another list, you can either use the extend() or insert() with for loop.

1. Quick Examples of Append List to a List

Following are quick examples of appending a list to another list.

 # Append list into another list languages1.append(languages2) # Append multiple lists into another list languages1.append([languages2,languages3]) # Append list elements to list languages1.extend(languages2) # Append List Elements using insert() for x in languages2: languages1.insert(len(languages1),x) 

2. Append List as an Element into Another List

To append the python list as an element into another list, you can use the append() from the list. This takes either string, number, or iterable as an argument and appends it at the end of the list.

 # Consider two lists languages1=['Python','PHP','Java',] languages2=['C','C++','C#'] print("Language List 1: ",languages1) print("Language List 2: ",languages2) # Append list into another list languages1.append(languages2) print("Result: ",languages1) 

This example yields the below output.

Читайте также:  Html table border colors css

python list insert another list

Similarly, you can also append multiple lists to another list.

 # Consider three lists languages1=['Python','PHP','Java',] languages2=['C','C++','C#'] languages3=["Scala","Ruby"] print("Language List 1: ",languages1) print("Language List 2: ",languages2) print("Language List 3: ",languages3) # Append multiple lists into another list languages1.append([languages2,languages3]) print("Result: ",languages1) 
 # Output: Language List 1: ['Python', 'PHP', 'Java'] Language List 2: ['C', 'C++', 'C#'] Language List 3: ['Scala', 'Ruby'] Result: ['Python', 'PHP', 'Java', [['C', 'C++', 'C#'], ['Scala', 'Ruby']]] 

3. Append List Elements to Another List

If you wanted to append the individual element from one list to another list you can use the extend(). This method takes the list as an argument and extends the list to separate elements and appends to the list.

 # Consider two lists languages1=['Python','PHP','Java',] languages2=['C','C++','C#'] print("Language List 1: ",languages1) print("Language List 2: ",languages2) # Append List Elements into Another List languages1.extend(languages2) print("Result: ",languages1) 
 # Output: Language List 1: ['Python', 'PHP', 'Java'] Language List 2: ['C', 'C++', 'C#'] Result: ['Python', 'PHP', 'Java', 'C', 'C++', 'C#'] 

Similarly, you can also get this output by using + operator * unpack and many more. Refer to append multiple elements to the list.

4. Using Looping with insert()

Finally, you can also achieve this by using the insert() with for loop. This is used to append a single element at a time at a specific position. Here, we will loop through the languages2 list and each element is appended to the languages1 at the end. len(languages2) returns the count of the list which is used to specify the end position.

 # Consider two lists languages1=['Python','PHP','Java',] languages2=['C','C++','C#'] print("Language List 1: ",languages1) print("Language List 2: ",languages2) # Append list elements using insert() for x in languages2: languages1.insert(len(languages1),x) print("Result: ",languages1) 

This yields the same output as above.

Conclusion

In this article, you have learned how to append the whole list as an item or element to another list using append() and also learned appending elements from one list to another using extend() and other approaches.

You may also like reading:

Источник

Python : How to add an element in list ? | append() vs extend()

In this article we will discuss how to add element in an existing list using different techniques.

Adding item in list using list.append()

It adds the item at the end of list.

For example, we have a list of string i.e.

# List of string wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

Now let’s add an element at the end of this list using append() i.e.

Frequently Asked:

''' Adding item in list using list.append() ''' wordList.append("from")
['hi', 'hello', 'this', 'that', 'is', 'of', 'from']

Passing an another list as a parameter in list.append()

As list can contain different kind of elements, so if we pass an another list object as parameter in append() i.e.

''' Passing an another list as a parameter in list.append() ''' wordList.append(["one", "use", "data"])

Then whole list object will be added to the end of list. So, list contents will be now,

['hi', 'hello', 'this', 'that', 'is', 'of', 'from', ['one', 'use', 'data']]

Adding all elements of one list to another using list.extend()

It will add all elements of list1 at the end of list. Basically it will merge the two lists i.e.

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of'] ''' Adding all elements of one list to another using list.extend() ''' wordList.extend(["one", "use", "data"])

Now list contents will be,

['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']

list append() vs extend()

list.append(item) , considers the parameter item as an individual object and add that object in the end of list. Even if given item is an another list, still it will be added to the end of list as individual object i.e.

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of'] wordList.append(["one", "use", "data"])
['hi', 'hello', 'this', 'that', 'is', 'of', ['one', 'use', 'data']]

list.extend(item) , considers parameter item to be an another list and add all the individual elements of the list to the existing list i.e.

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of'] wordList.extend(["one", "use", "data"])
['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']

Complete example is as follows,

""" Python : How to add element in list | append() vs extend() """ def main(): # List of string wordList = ['hi', 'hello', 'this', 'that', 'is', 'of'] # print the List print(wordList) ''' Adding item in list using list.append() ''' wordList.append("from") # print the List print(wordList) ''' Passing an another list as a parameter in list.append() ''' wordList.append(["one", "use", "data"]) # print the List print(wordList) wordList = ['hi', 'hello', 'this', 'that', 'is', 'of'] ''' Adding all elements of one list to another using list.extend() ''' wordList.extend(["one", "use", "data"]) # print the List print(wordList) if __name__ == "__main__": main()
['hi', 'hello', 'this', 'that', 'is', 'of'] ['hi', 'hello', 'this', 'that', 'is', 'of', 'from'] ['hi', 'hello', 'this', 'that', 'is', 'of', 'from', ['one', 'use', 'data']] ['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

4 Easy Ways to Add/Append List to List in Python

Here are 4 methods to add/append a list to a list in Python:

  1. Using list.extend() method
  2. Using list.append() method
  3. Using list.insert() method
  4. Using list concatenation

Method 1: Using the list.extend() function

The easiest way to append/add a list to a list in Python is to use the “list.extend()” method. The list.extend() is a built-in function that adds all the items of an iterable (list, tuple, string, etc.) to the end of the list.

Syntax

Parameters

iterable: The extend() method takes an iterable such as a list, tuple, string, etc.

Return Value

The extend() method modifies the original list. It doesn’t return any value.

Example

listA = ["Millie", "Caleb", "Gaten"] listB = ["Finn", "Noah", "Sadie"]

Append the listB to a listA using the extend() function.

listA = ["Millie", "Caleb", "Gaten"] listB = ["Finn", "Noah", "Sadie"] listA.extend(listB) print(listA)
['Millie', 'Caleb', 'Gaten', 'Finn', 'Noah', 'Sadie']

Method 2: Using the list.append() function

You can use the “list.append()” method to append a list into a list as an element. The list append() is a built-in function that adds a single element to the existing list.

Syntax

Parameters

The list.append() function takes a single item and adds it to the end of the list.

Example

Let’s take the above example, and instead of using extend() method, let’s use the append() function.

listA = ["Millie", "Caleb", "Gaten"] listB = ["Finn", "Noah", "Sadie"] listA.append(listB) print(listA)
['Millie', 'Caleb', 'Gaten', ['Finn', 'Noah', 'Sadie']]

Here you can see that the second list is added as a single element to the existing list.

Method 3: Using list.insert() Method

The insert() method inserts the list before the given index.

Example

# Define the initial list list1 = [1, 2, 3] # Define the list to be added list2 = [4, 5, 6] # Insert list2 into list1 at index 1 list1.insert(1, list2) print(list1) 

Method 4: Using list concatenation

To concatenate a list in Python, you can use the “+” operator.

Example

# Define the initial list list1 = [1, 2, 3] # Define the list to be added list2 = [4, 5, 6] list3 = list1 + list2 print(list3) 

Источник

Оцените статью