Table of Contents
ToggleJoining lists in Python refers to combining the elements of multiple lists into a single list. This can be achieved using various methods, such as concatenation, list comprehension, or using built-in functions like extend() or the + operator. Joining lists does not modify the original lists but creates a new list containing the combined elements.
The concatenation operator in Python, denoted by +, is used to join two sequences, such as strings, lists, or tuples, into a single sequence. When applied to lists, the concatenation operator joins the elements of the two (or more) lists to create a new list containing all the elements from both lists.
# Two lists to be joined
L1 = [10, 20, 30, 40]
L2 = ['one', 'two', 'three', 'four']
# Joining the lists
joined_list = L1 + L2
# Printing the joined list
print("Joined List:", joined_list)
Output:
Joined List: [10, 20, 30, 40, 'one', 'two', 'three', 'four']
List comprehension is a concise way to create lists in Python. It is used to generate new lists by applying an expression to each item in an existing iterable, such as a list, tuple, or range. The syntax for list comprehension is:
new_list = [expression for item in iterable]
This creates a new list where expression is evaluated for each item in the iterable.
# Two lists to be joined
L1 = [36, 24, 3]
L2 = [84, 5, 81]
# Joining the lists using list comprehension
joined_list = [item for sublist in [L1, L2] for item in sublist]
# Printing the joined list
print("Joined List:", joined_list)
Output:
Joined List: [36, 24, 3, 84, 5, 81]
The append() function in Python is used to add a single element to the end of a list. This function modifies the original list by adding the element to the end of the list.
We can join a list using the append() function by iterating over the elements of one list and appending each element to another list.
# List to which elements will be appended
list1 = ['Fruit', 'Number', 'Animal']
# List from which elements will be appended
list2 = ['Apple', 5, 'Dog']
# Joining the lists using the append() function
for element in list2:
list1.append(element)
# Printing the joined list
print("Joined List:", list1)
Output:
Joined List: ['Fruit', 'Number', 'Animal', 'Apple', 5, 'Dog']
The extend() function in Python is used to append elements from an iterable (such as another list) to the end of the list. This function modifies the original list in place, adding the elements of the iterable to the end of the list.
We can join a list using the extend() function by calling it on one list and passing another list (or any iterable) as an argument. This will append all the elements from the second list to the end of the first list.
# List to be extended
list1 = [10, 15, 20]
# List to be added
list2 = [25, 30, 35]
# Joining the lists using the extend() function
list1.extend(list2)
# Printing the extended list
print("Extended List:", list1)
Output:
Extended List: [10, 15, 20, 25, 30, 35]
Enroll in our Python certification course at Vista Academy and become a certified expert to boost your career.
Enroll Now