How to Add Elements to a List in Python (append, extend and insert)

[ad_1]

When working with lists in Python, you will often want to add new elements to the list.

The Python list data type has three methods for adding elements:

  • append() – appends a single element to the list.
  • extend() – appends elements of an iterable to the list.
  • insert() – inserts a single item at a given position of the list.

All three methods modify the list in place and return None.

Python List append() #

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

The syntax of the append() method is as follows:

Where, element is the element to be added to the list.

Here is an example:

characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

characters.append('Nairobi')

print('Updated list:', characters)
Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi']

The element parameter can be an object of any data type:

odd_numbers = [1, 3, 5, 7] 

even_numbers = [2, 4, 6]

odd_numbers.append(even_numbers)

print('Updated list:', odd_numbers)

The list even_numbers is added as a single element to the odd_numbers list.

Updated list: [1, 3, 5, 7, [2, 4, 6]]

Python List extend() #

The extend() method all elements of an iterable to the end of the list.

The syntax of the extend() method is as follows:

Where, iterable is the iterable to be added to the list.

characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

new_characters = ['Nairobi', 'Denver', 'Rio']

characters.extend(new_characters)

print('Updated list:', characters)
Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi', 'Denver', 'Rio']

The argument can be any type of iterable:

animals = ['dog', 'cat']

# tuple
mammals = ('tiger', 'elephant')

animals.extend(mammals)

print('Updated list:', animals)

# dictionary
birds = {'owl': 1, 'parrot': 2}

animals.extend(birds)

print('Updated list:', animals)
Updated list: ['dog', 'cat', 'tiger', 'elephant']
Updated list: ['dog', 'cat', 'tiger', 'elephant', 'owl', 'parrot']

Python List insert() #

The insert() method adds a single element to the list at the specified index.

The syntax of the insert() method is as follows:

list.insert(index, element) 

Where, index is the index of the element before which to insert, and the element is the element to be inserted in the list. In Python the list index starts with 0.

fruits = ['raspberry', 'strawberry', 'blueberry'] 

fruits.insert(1, 'cranberry')

print('Updated list:', fruits)
Updated list: ['raspberry', 'cranberry', 'strawberry', 'blueberry']

The element parameter can be an object of any data type:

numbers = [10, 15, 20, 25] 

squares = [1, 4, 9]

numbers.insert(2, squares)

print('Updated list:', numbers)

The list squares is inserted as a single element to the numbers list.

Updated list: [10, 15, [1, 4, 9], 20, 25]

Conclusion #

We have shown you how to add elements to a list in Python using the append(), extend(), and insert() methods. Another way to add elements to a list is to use the + operator to concatenate multiple lists.

If you have any questions or feedback, feel free to leave a comment.

[ad_2]

Source link

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.