Linux

How to Add Elements to a List in Python (add, add and enter)

How to Add Elements to a List in Python (add, add and enter)

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 () – adds one element to the list.
  • extend () – adds an iterable element to the list.
  • insert () – inserts an item at a certain position from the list.

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

List of Python append ()

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

The append () method syntax is as follows:

list.append(element) 

Where, elements are elements that will be added to the list.

Here is an example:

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

characters.append('Nairobi')

print('Updated list:', characters)

Output :

Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi']

Element parameters can be objects 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 even_numbers list is added as a single element to the odd_numbers list.

Output :

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

List of Python extended ()

The extended () method is all elements that can be repeated to the end of the list.

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

list.extend(iterable) 

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

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

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

characters.extend(new_characters)

print('Updated list:', characters)

Output :

Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi', 'Denver', 'Rio']

Arguments 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)

Output :

Updated list: ['dog', 'cat', 'tiger', 'elephant']
Updated list: ['dog', 'cat', 'tiger', 'elephant', 'owl', 'parrot']

Insert Python List ()

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.

Here is an example:

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

fruits.insert(1, 'cranberry')

print('Updated list:', fruits)

Output :

Updated list: ['raspberry', 'cranberry', 'strawberry', 'blueberry']

Element parameters can be objects of any data type:

numbers = [10, 15, 20, 25] 

squares = [1, 4, 9]

numbers.insert(2, squares)

print('Updated list:', numbers)

The list box is entered as a single element into a number list.

Output :

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 (), extended (), and insert () methods. Another way to add elements to a list is to use the + operator to join several lists.

If you have questions or feedback, don’t hesitate to leave a comment.

Related posts

How to Change the Time Zone in Ubuntu 20.04

Linux

How to install Zulip chat server on Ubuntu 18.04

Linux

How to Install Apache Maven on Ubuntu 20.04

Linux

How to Install Jenkins on Ubuntu 20.04 with Nginx and SSL

Linux

How to Download a Linux ISO File via a Torrent Client

Linux

How to Set Up WireGuard VPN Server and Client on Ubuntu 20.04

Linux

How to compile the ONLYOFFICE Document Server from source code on Ubuntu

Linux

Fedora 31: How to Install Fuel CMS with Nginx

Howto

How to Install Steam on Ubuntu 20.04

Linux