HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Python - Add List Items


In Python, you can add items to a list using various methods such as append(), extend(), or concatenation (+). Here's how you can add items to a list:

Append Items

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

Example
# Creating a list
my_list = ['apple', 'banana', 'cherry']

# Adding a single item using append()
my_list.append('date')

print(my_list)  # Output: ['apple', 'banana', 'cherry', 'date']
                 
             

extend()Items

Using extend() method to add multiple items (another list) at the end of the list:

Example
# Creating a list
my_list = ['apple', 'banana', 'cherry']

# Adding multiple items using extend()
my_list.extend(['date', 'elderberry'])

print(my_list)  # Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']

                 
             

concatenation (+)

Using concatenation (+) to join two lists:

Example
# Creating a 
my_list = ['apple', 'banana', 'cherry']

# Adding multiple items using concatenation
my_list += ['date', 'elderberry']

print(my_list)  # Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']