Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

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

designlinux 0 Comments

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:

list.append(element) 

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:

list.extend(iterable) 

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.

Here is an example:

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.

python

Related

Tags: python

How to Install Skype on Ubuntu 20.04 LTS

Prev Post

How to Delete (Remove) Symbolic Links in Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 72
  • 605,822

DesignLinux.com © All rights reserved

Go to mobile version