• Python Basics

  • Python Variables

  • Operators in Python

  • Conditional Statements in Python

  • Python Lists

  • Python Tuples

  • Python Sets

  • Python Dictionaries

  • Loops in Python

  • Python Arrays and Functions

  • Conclusion

Python Join List

Join List Python

In Python, joining or concatenating lists allows you to combine multiple lists into one. There are several methods to achieve this, including using the + operator, appending elements one by one, and using the extend() method. This lesson will cover these methods in detail.

1. Join Two Lists Using the + Operator

One of the simplest ways to join two lists is by using the + operator. This operator creates a new list that is the result of concatenating the two lists.

Example: Join Two Lists with +

python
1list1 = ["a", "b", "c"]
2list2 = [1, 2, 3]
3
4list3 = list1 + list2  # Joining list1 and list2
5print(list3)  # Output: ['a', 'b', 'c', 1, 2, 3]

2. Join Lists by Appending Items

Another way to join two lists is by appending all the items from the second list into the first list, one by one. This method modifies the first list in place.

Example: Append List2 into List1

python
1list1 = ["a", "b", "c"]
2list2 = [1, 2, 3]
3
4for x in list2:
5    list1.append(x)  # Appending each item from list2 to list1
6
7print(list1)  # Output: ['a', 'b', 'c', 1, 2, 3]

3. Using the extend() Method

The extend() method is specifically designed for joining lists. It adds all elements from one list to the end of another list. This method modifies the original list and does not return a new list.

Example: Use the extend() Method

python
1list1 = ["a", "b", "c"]
2list2 = [1, 2, 3]
3
4list1.extend(list2)  # Extending list1 with elements from list2
5print(list1)  # Output: ['a', 'b', 'c', 1, 2, 3]

Frequently Asked Questions