Python Join Lists | What is Join and Why to Join Lists

Python Join Lists

In Python, joining lists means combining two or more lists into a single list. This is useful when you want to merge data or extend a list.

Python में lists को join करना मतलब दो या ज्यादा lists को एक साथ जोड़ना। यह तब काम आता है जब आपको data merge या list को बढ़ाना हो।

Why Join Lists?

  • To combine related data from multiple lists.
  • To simplify operations by having all items in one list.
  • To prepare data for processing or output.
  • कई lists से related data को एक साथ लाने के लिए।
  • सभी items को एक list में रखकर काम आसान बनाने के लिए।
  • डेटा को process या output के लिए तैयार करने के लिए।
Example 1: Using + Operator
list1 = [1, 2, 3]
list2 = [4, 5, 6]
joined = list1 + list2
print(joined)

Join two lists using the + operator.

+ operator से दो lists को जोड़ना।

Output:

[1, 2, 3, 4, 5, 6]
Example 2: Using extend() Method
list1 = ['a', 'b']
list2 = ['c', 'd']
list1.extend(list2)
print(list1)

Add all elements of list2 to list1 using extend().

extend() से list2 के elements को list1 में जोड़ना।

Output:

['a', 'b', 'c', 'd']
Example 3: Using * Operator (Python 3.5+)
list1 = [1, 2]
list2 = [3, 4]
list3 = [*list1, *list2]
print(list3)

Unpack and join lists using * operator.

* operator से lists को unpack कर जोड़ना।

Output:

[1, 2, 3, 4]
Example 4: Using list() and itertools.chain()
import itertools
list1 = [1, 2]
list2 = [3, 4]
joined = list(itertools.chain(list1, list2))
print(joined)

Join lists using itertools.chain().

itertools.chain() से lists को जोड़ना।

Output:

[1, 2, 3, 4]
Example 5: Using List Comprehension
list1 = [1, 2]
list2 = [3, 4]
joined = [x for l in (list1, list2) for x in l]
print(joined)

Join lists using nested list comprehension.

nested list comprehension से lists को जोड़ना।

Output:

[1, 2, 3, 4]