How to append using Python?

0132

In Python, you can append elements to a list using the append() method. Here’s a simple example:

# Create a list
my_list = [1, 2, 3]

# Append an element
my_list.append(4)

print(my_list)  # Output: [1, 2, 3, 4]

You can also append multiple elements by using the extend() method or by using the += operator:

# Using extend() to append multiple elements
my_list.extend([5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

# Using += operator
my_list += [7, 8]
print(my_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8]

Feel free to ask if you have more questions!

0 Comments

no data
Be the first to share your comment!