• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Union of Lists in Python

Union of Lists in Python

February 21, 2022 Leave a Comment

In Python, the easiest way to get the union of two lists is by using the + operator to add the two lists together. You can then remove the duplicates from the result by converting it to a set, and then converting that set back to a list.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

union_of_lists = list(set(list_1 + list_2))

print(union_of_lists)

#Output:
[1, 2, 3, 4, 5, 8, 9]

If you want to find the union of more than two lists, we can easily do that in Python with a simple function.

def union(lists):
    all_elements = []
    for x in lists:
        all_elements = all_elements + x
    return list(set(all_elements))

list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
list4 = [5,3,6,8,2,2,0]

print(union([list1,list2,list3,list4]))

#Output:
[0, 1, 2, 3, 4, 5, 6, 8, 9]

When working with multiple lists, it can be useful to find the entire collection of elements which exists in all of your lists. The union of two or more lists is a list of all elements from all of the lists.

So, for example, if we have a list A and a list B, then the union of A and B is a list of all elements from both A and B. We combine all items of the two lists with the union.

We can get the union of two lists in Python easily.

To get the union of lists in Python, we can combine lists with the + operator. Then, we can convert the resulting list to a set, and back to a list to get the unique list of all elements in all of the lists.

Below is an example of how to get the union of two lists in Python.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

union_of_lists = list(set(list_1 + list_2))

print(union_of_lists)

#Output:
[1, 2, 3, 4, 5, 8, 9]

The example above gives us a sorted list with no duplicates. If you want to include duplicates, or maintain the order, there are a few other ways you can get the union of a list.

If you’d instead like to get the intersection of multiple lists, you can read our article about finding the intersection of lists.

Finding the Union of Lists and Keep Duplicates in Python

In Python, finding the union of multiple lists and keeping any duplicates is easy.

To combine multiple lists in Python and keep the duplicates of all of the items, you can simply use the + operator.

Below is an example of how to find the union of all lists keeping all duplicates.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

union_of_lists = list_1 + list_2

print(union_of_lists)

#Output:
[5, 3, 8, 2, 1, 9, 3, 4, 2, 1]

If you want to sort this result, then you can use the Python sorted() function or the sort() function.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

union_of_lists = list_1 + list_2

print(sorted(union_of_lists))

#Output:
[1, 1, 2, 2, 3, 3, 4, 5, 8, 9]

How to Find the Union of More Than Two Lists in Python

So far in this article, we’ve only found the union of two lists. We can easily find the union of more than two lists in Python.

To do so, we can define a function which takes in a number of lists, and then finds the union of all of the lists.

Below is an example of how to find the union of more than two lists in Python.

def union(lists):
    all_elements = []
    for x in lists:
        all_elements = all_elements + x
    return list(set(all_elements))

list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
list4 = [5,3,6,8,2,2,0]

print(union([list1,list2,list3,list4]))

#Output:
[0, 1, 2, 3, 4, 5, 6, 8, 9]

Another method to find the union of more than two lists using Python is if you know how many lists you are working with. The example above is good for any number of lists, but if you have three lists, you can use the set union() function.

Below is another example of how to find the union of multiple lists with Python.

def union(list1,list2,list3):
    union_of_lists = list(set().union(list1, list2, list3))
    return union_of_lists 

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

print(union(list1,list2,list3))

#Output:
[0, 1, 2, 3, 4, 5, 6, 8, 9]

Hopefully this article has been useful for you to learn how to use Python to get the union of lists.

Other Articles You'll Also Like:

  • 1.  Python min() function – Get Minimum of List or Dictionary with min() Function
  • 2.  Truncate String in Python with Slicing
  • 3.  Create Empty List in Python
  • 4.  Convert Set to List in Python
  • 5.  Loop Through Files in Directory Using Python
  • 6.  Pandas Crosstab on Multiple Columns
  • 7.  Keep Every Nth Element in List in Python
  • 8.  Scroll Down Using Selenium in Python
  • 9.  How to Remove NaN from List in Python
  • 10.  Using readlines() and strip() to Remove Spaces and \n from File in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy