Append list into list python

How to append list to another list in Python?

Python – Append list to another list using extend()

To append a list to another list, use extend() function on the list you want to extend and pass the other list as argument to extend() function.

In this tutorial, we shall learn the syntax of extend() function and how to use this function to append a list to other list.

Syntax of extend()

Following is the syntax of extend() function.

where elements of list2 are appended to the elements of list1.

extend() does inplace update to the original list list1. The function returns None.

Examples

1. Append a list to another list

In the following example, we create two lists: list1 and list2, and append the second list list2 to the first one list1.

Читайте также:  Python авторизация на yandex

Python Program

# Take two lists list1 = [6, 52, 74, 62] list2 = [85, 17, 81, 92] # Extend first list with the second one list1.extend(list2) # Print the first list print(list1)

The contents of the list1 are modified.

2. Append a list to another list keeping a copy of original list

If you would like to keep the contents of original list unchanged, copy the list to a variable and then add the other list to it.

Python Program

# Take two lists list1 = [6, 52, 74, 62] list2 = [85, 17, 81, 92] # Make of copy of list1 result = list1.copy() # Append the second list to first list result.extend(list2) # Print the first list print(result)

list1 is preserved while the resulting extended list is in result.

3: Append a list to another list using For Loop

You can also use a For Loop to iterate over the elements of second list, and append each of these elements to the first list using list.append() function.

Python Program

# Take two lists list1 = [6, 52, 74, 62] list2 = [85, 17, 81, 92] # Append each item of list2 to list1 for item in list2: list1.append(item) # Print the first list print(list1)

Summary

In this tutorial of Python Examples, we learned how to extend a list with another list appended to it, with the help of well detailed example programs.

Источник

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.

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 Append List to another List without Brackets

In this Python tutorial, we will learn about the Python Append List to another List without Brackets where we will see various ways to Append a List to another List without Brackets using the examples.

Python append list to another list

Python programmers often encounter – appending one list to another. To start, let’s understand what appending a list in Python usually means.

When we talk about appending a list to another list in Python, it usually involves adding the second list as a single element at the end of the first list. This is done using the append() method. However, this method results in a nested list, i.e., a list within a list.

east_states = ['New York', 'Florida', 'Georgia'] west_states = ['California', 'Washington', 'Nevada'] east_states.append(west_states) print(east_states) 

Python Append List to another List without Brackets

Notice the extra brackets around the second list in the output. This means that east_states is nested inside west_states. Sometimes, you want to merge two lists without nesting, meaning you want to add each element of the second list to the first list as an individual element.

So, let’s move ahead and learn different ways to append List to another List without Brackets.

Append List to another List without Brackets in Python

Today, we’re going to dive into a common scenario American developers often deal with: adding one list to another, but without the added brackets that typically result.

To kick off, let’s imagine that we’re on a road trip, traveling through various states. We have two lists of states, one for the eastern states we’ve visited, and one for the western states.

But, this time we just want one big list of all states we’ve visited, without categorizing them as east or west. So, let’s discuss each of the methods:

Method1: Using Python Extend() Function

east_states = ['New York', 'Florida', 'Georgia'] west_states = ['California', 'Washington', 'Nevada'] east_states.extend(west_states) print(east_states) 

Python append list to another list

Now you can see, extend() adds each element of east_states to west_states resulting in a single list without extra brackets. In other words, extend() merges the two lists together. We’ve effectively created a list of all states visited during our road trip!

Method2: += operator in Python

An alternative to the extend() method is the += operator, which can be used to achieve the same effect.

east_states = ['New York', 'Florida', 'Georgia'] west_states = ['California', 'Washington', 'Nevada'] east_states += west_states print(east_states) 

Append List to another List without Brackets in Python

As you can see, the += operator also combines the lists into one, just like extend() , without any extra brackets or nesting.

Conclusion

In conclusion, Python provides different methods to append one list to another. While append() nests the second list within the first, extend() and += operator adds the second list to the first as individual elements, removing the need for additional brackets.

You may also like to read the following Python tutorials.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

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