Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Join Lists (Complete Guide for Beginners)

In Python, joining lists means combining two or more lists into one single list. This is a very common operation when working with data, such as merging results, combining user inputs, or building datasets.

Python provides multiple simple ways to join lists depending on your needs.


🔹 What is Joining Lists in Python?

Joining lists means:

Taking two or more separate lists and merging them into one list.

Example:

[1, 2] + [3, 4]  [1, 2, 3, 4]

🔹 1. Using + Operator (Most Common Method)

The easiest way to join lists is using the plus operator.

Syntax:

new_list = list1 + list2

🔹 Example 1: Join Two Lists

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

result = list1 + list2

print(result)

Output:

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

✔ Simple and readable
✔ Creates a new list


🔹 2. Using extend() Method

The extend() method adds elements of one list into another list.

Syntax:

list1.extend(list2)

🔹 Example 2: Using extend()

list1 = [10, 20, 30]
list2 = [40, 50, 60]

list1.extend(list2)

print(list1)

Output:

[10, 20, 30, 40, 50, 60]

✔ Modifies original list
✔ Very efficient


🔹 Difference Between + and extend()

Feature+ Operatorextend()
Creates new list           ✅ Yes             ❌ No
Modifies original list           ❌ No             ✅ Yes
Performance           Medium             Fast
Best use           When you want new list             When updating existing list

🔹 3. Using Loop to Join Lists

You can manually join lists using a loop.

Example 3: Using for loop

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

for item in list2:
list1.append(item)

print(list1)

Output:

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

✔ Good for custom logic
❌ Not recommended for simple tasks


🔹 4. Using * Operator (Unpacking)

Python also supports list unpacking using *.

Syntax:

new_list = [*list1, *list2]

🔹 Example 4: Using unpacking

list1 = ["a", "b"]
list2 = ["c", "d"]

result = [*list1, *list2]

print(result)

Output:

['a', 'b', 'c', 'd']

✔ Modern Python style
✔ Very clean and fast


🔹 5. Joining Multiple Lists

You can join more than two lists easily.

Example 5:

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

result = list1 + list2 + list3

print(result)

Output:

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

🔹 6. Real-Life Example

Merging student lists from different classes:

class_a = ["John", "Alice"]
class_b = ["Bob", "Emma"]

all_students = class_a + class_b

print(all_students)

Output:

['John', 'Alice', 'Bob', 'Emma']

🔹 7. Joining Lists with Numbers (Advanced Example)

numbers = [1, 2, 3]
more_numbers = [4, 5, 6]

numbers.extend(more_numbers)

print(numbers)

🔹 Key Points to Remember

  • + → creates new list
  • extend() → modifies existing list
  • * unpacking → modern and clean method
  • Loop method → flexible but slower
  • All methods work with strings, numbers, and mixed data

🚀 Conclusion

Python makes it very easy to join lists using multiple methods. For beginners, the + operator is the simplest, while extend() is best for performance when modifying existing lists.

For modern Python code, list unpacking (*) is also a clean and powerful option.

Mastering list joining helps you manage and combine data efficiently in real-world projects. 




Post a Comment

0 Comments