Python append item to list in list

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 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.

Читайте также:  Python from future unicode

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 — Add List Items

To add an item to the end of the list, use the append() method:

Example

Using the append() method to append an item:

Insert Items

To insert a list item at a specified index, use the insert() method.

The insert() method inserts an item at the specified index:

Example

Insert an item as the second position:

Note: As a result of the examples above, the lists will now contain 4 items.

Extend List

To append elements from another list to the current list, use the extend() method.

Example

Add the elements of tropical to thislist :

thislist = [«apple», «banana», «cherry»]
tropical = [«mango», «pineapple», «papaya»]
thislist.extend(tropical)
print(thislist)

The elements will be added to the end of the list.

Add Any Iterable

The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.).

Example

Add elements of a tuple to a list:

thislist = [«apple», «banana», «cherry»]
thistuple = («kiwi», «orange»)
thislist.extend(thistuple)
print(thislist)

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Читайте также:  Php get all classes that extend

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

How To add Elements to a List in Python

How To add Elements to a List in Python

In this tutorial, we will learn different ways to add elements to a list in Python.

There are four methods to add elements to a List in Python.

  1. append() : append the element to the end of the list.
  2. insert() : inserts the element before the given index.
  3. extend() : extends the list by appending elements from the iterable.
  4. List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.

Prerequisites

In order to complete this tutorial, you will need:

This tutorial was tested with Python 3.9.6.

append()

This function adds a single element to the end of the list.

fruit_list = ["Apple", "Banana"] print(f'Current Fruits List fruit_list>') new_fruit = input("Please enter a fruit name:\n") fruit_list.append(new_fruit) print(f'Updated Fruits List fruit_list>') 
Current Fruits List ['Apple', 'Banana'] Please enter a fruit name: Orange Updated Fruits List ['Apple', 'Banana', 'Orange'] 

This example added Orange to the end of the list.

insert()

This function adds an element at the given 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>') 
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] 

This example added 20 at the index of 2 . 20 has been inserted into the list at this index.

extend()

This function adds iterable elements to the list.

extend_list = [] extend_list.extend([1, 2]) # extending list elements print(extend_list) extend_list.extend((3, 4)) # extending tuple elements print(extend_list) extend_list.extend("ABC") # extending string elements print(extend_list) 
[1, 2] [1, 2, 3, 4] [1, 2, 3, 4, 'A', 'B', 'C'] 

This example added a list of [1, 2] . Then it added a tuple of (3, 4) . And then it added a string of ABC .

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] 

This example added the list of evens to the end of the list of odds . The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

Conclusion

Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and 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

Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!

Источник

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