Skip to content

Python List Union with Example Malli Spark By {Examples}

  • by

How to get a union of two or multiple lists in Python? Getting the union of lists depends on the specific requirements of the problem at hand. Sometimes you need to maintain the order and repetition, while other times you need to remove duplicates and sort the final list. It is important to carefully consider the specific requirements of the problem and choose the appropriate method to get the desired output.

You can get a union of lists in Python using many ways, for example, by using the for loop, union(), set(), + operator, | operator, itertools and extend() functions. In this article, I will explain how to get a union of lists by using all these methods with examples.

1. Quick Examples of Union of List

If you are in a hurry, below are some quick examples of how to get a union of lists.

# Quick examples of union of list

# Example 1: Get union of lists
# Using for loop
newList = []
newList.extend(mylist1)
for element in mylist2:
if element not in newList:
newList.append(element)

# Example 2: Get union of lists
# Using for loop and modifying input mylist1
for element in mylist2:
if element not in mylist1:
mylist1.append(element)

# Example 3: Get a union of lists
# Using sets
myset1 = set(mylist1)
myset2 = set(mylist2)
newList = list(myset1.union(myset2))

# Example 4: Remove duplicates
# Using the set() function
union_list = list(set(concatenated_list))

# Example 5: Using | operator
def Union(mylist1, mylist2):
final_list = list(set(mylist1) | set(mylist2))
return final_list
result = Union(mylist1, mylist2)

# Example 6: Maintaining both repetition and order
def Union(mylist1, mylist2):
final_list = sorted(mylist1 + mylist2)
return final_list
result = Union(mylist1, mylist2)

# Example 7: Get a union of lists
# Using itertools
def union_lists(*lists):
concatenated_list = list(itertools.chain(*lists))
# Use the set() function to remove duplicates
unique_set = set(concatenated_list)
final_union = list(unique_set)
return final_union

# Example 8: Using extend() method
mylist1.extend(mylist2)
# Not maintaining repetition and order
result = list(set(mylist1))

2. Get a Union of Lists Using For Loop

You can find the union of two or more lists using a for loop in Python. first, initialize two lists mylist1 and mylist2 with some values, iterates over each element in mylist2 using a for loop and then add the element to the first list mylist1 when an element is not present in mylist1.

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Get union of lists
# Using for loop and modifying input mylist1
for element in mylist2:
if element not in mylist1:
mylist1.append(element)
print(“Union of the lists is:”, mylist1)

Yields below output.

Alternatively, you can also creat the new list by combining elements from both the list.

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Get union of lists
# Using for loop
newList = []
newList.extend(mylist1)
for element in mylist2:
if element not in newList:
newList.append(element)
print(“Union of the lists is:”, newList)

3. Get a Union of Lists Using Sets

You can also get the union of two lists using sets in Python. For example, two lists mylist1 and mylist2 are initialized with some elements. Then, the set() function is used to convert each list into a set myset1 and myset2, respectively. After that, the union() method is used to get the union of the two sets, and the resulting set is converted back to a list newList using the list() constructor.

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Get a union of lists
# Using sets
myset1 = set(mylist1)
myset2 = set(mylist2)
newList = list(myset1.union(myset2))
print(“Union of the lists is:”, newList)

Yields the same output as above.

4. Get a Union of Lists Using the + operator

You can also get the union of two lists in Python using + operator to concatenate the two lists, and then remove duplicates using the set() function.

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Concatenate the two lists
# Using the + operator
concatenated_list = mylist1 + mylist2

# Remove duplicates
# Using the set() function
union_list = list(set(concatenated_list))
print(“Union of the lists is:”, union_list)

In the above example, two lists mylist1 and mylist2 are initialized with some elements. The + operator is used to concatenate the two lists into a single list concatenated_list. Then, the set() function is used to remove duplicates from concatenated_list, and the resulting set is converted back to a list union_list using the list() constructor.

Yields the same output as above.

4.1 Using | Operator Without Repetition

In this approach, the set() function is used to convert each input list to a set, which automatically removes any duplicates. Then, the | operator is used to perform a set union operation on the two sets, which produces a new set that contains all unique elements from both input sets.

# Using | operator
def Union(mylist1, mylist2):
final_list = list(set(mylist1) | set(mylist2))
return final_list
result = Union(mylist1, mylist2)
print(“Union of the lists is:”, result)

Yields the same output as above.

4.2 Maintaining Both Repetition and Order

In this apprach, the + operator is used to concatenate the two input lists into a new list. Then, the sorted() function is used to sort the resulting list in ascending order, which produces a new list that contains all unique elements from both input lists in sorted order.

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Maintaining both repetition and order
def Union(mylist1, mylist2):
final_list = sorted(mylist1 + mylist2)
return final_list

result = Union(mylist1, mylist2)
print(“Union of the lists is:”, result)

# Output:
# Original list1: [1, 2, 3, 4]
# Original list2: [2, 5, 1, 3]
# Union of the lists is: [1, 1, 2, 2, 3, 3, 4, 5]

5. Get a Union of Lists Using itertools

You can also use the itertools module in Python to get the union of two or more lists. For example, defines a function named union_lists that accepts any number of lists as arguments and then returns a new list that contains all unique elements from all of the input lists. To accomplish this, the function first concatenates all the input lists into a single list using the itertools.chain() function, which takes multiple iterables as arguments and returns an iterator that produces the elements of each input iterable in sequence.

The resulting concatenated list is then converted to a set using the built-in set() function, which automatically removes any duplicate elements. Finally, the set is converted back to a list using the list() function, and this final list is returned as the output of the function.

import itertools

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Get a union of lists
# Using itertools
def union_lists(*lists):
concatenated_list = list(itertools.chain(*lists))
# Use the set() function to remove duplicates
unique_set = set(concatenated_list)
final_union = list(unique_set)
return final_union
print(“Union of the lists is:”, union_lists(mylist1, mylist2))

Yields the same output as above.

6. Using the extend() Method and the set() Function

You can also concatenate two lists using the extend() method and remove duplicates using the set() function. First, the extend() method is used to concatenate mylist2 to the end of mylist1. This modifies mylist1 in place, so that it now contains all elements from both input lists.

To remove duplicates and maintain the order of the remaining elements, the set() function is used to convert mylist1 to a set, which automatically removes any duplicates. Then, the resulting set is converted back to a list using the list() function, which produces a new list that contains all unique elements from mylist1 in the order they first appeared.

import itertools

# Initialize two lists
mylist1 = [1, 2, 3, 4]
mylist2 = [2, 5, 1, 3]

print(“Original list1: “, mylist1)
print(“Original list2: “, mylist2)

# Using extend() method
mylist1.extend(mylist2)

# Maintaining repetition
print(“Union of the lists maintaining repetition “, mylist1)

# Not maintaining repetition and order
result = list(set(mylist1))
print(“Union of the lists not maintaining repetition “, result)

# Output:
# Original list1: [1, 2, 3, 4]
# Original list2: [2, 5, 1, 3]
# Union of the lists maintaining repetition [1, 2, 3, 4, 2, 5, 1, 3]
# Union of the lists not maintaining repetition [1, 2, 3, 4, 5]

Conclusion

In this article, I have explained how to get a union of lists in Python by using the for loop, union(), set(), + operator, | operator, itertools, and extend() functions with examples.

Happy Learning !!

 How to get a union of two or multiple lists in Python? Getting the union of lists depends on the specific requirements of the problem at hand. Sometimes you need to maintain the order and repetition, while other times you need to remove duplicates and sort the final list. It is important to carefully consider  Read More Python, Python Tutorial, Python List Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *