Wednesday, January 15, 2025
HomeProgrammingHow To Add Two Lists In Python?

How To Add Two Lists In Python?

In Python, you can add two lists together using the + operator. This concatenates the lists, creating a new list containing the elements of both lists. Here’s an example:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

result = list1 + list2
print(result)

Output:

[1, 2, 3, 4, 5, 6]

This adds the elements of list2 to the end of list1, creating a new list.

See also  How do you merge one local branch into another local branch in Git?

Alternative: Using extend()

You can also use the extend() method to add elements of one list to another. This modifies the original list in place:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

list1.extend(list2)
print(list1)

Output:

[1, 2, 3, 4, 5, 6]


This method modifies list1 by adding the elements of list2 to it.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x