# Python program to find union and intersection of a list

### Problem Definition

Python program to create two lists by reading numbers from the user. Find the union and intersection of two lists. Display original, union, and intersection lists.

The union of list1 and list2 is a list, which contains the element present in either list1 or list2. The intersection of list1 and list2 is a list, which contains the element present in both list1and list2 that is common elements.

### Solution:

The following program demonstrates how to find the union of two lists and display the result in python.

```l1 = list()
l2 = list()
l3 = list()

n1 = int(input("Enter the number of elements in first list:"))
for i in range(n1):
ele = int(input("Enter the element of first list:"))
l1.append(ele)

print("First list is: ",l1)

n2 = int(input("Enter the number of elements in second list:"))
for i in range(n2):
ele = int(input("Enter the element of second list:"))
l2.append(ele)

print("second list is: ",l2)

l3.extend(l1)
for ele in l2:
if ele in l3:
continue
l3.append(ele)

print("Union of:", l1, "and ", l2, "is: ",l3)
```

### Output

```Enter the number of elements in first list:4
Enter the element of first list:2
Enter the element of first list:3
Enter the element of first list:4
Enter the element of first list:5
First list is:  [2, 3, 4, 5]
Enter the number of elements in second list:3
Enter the element of second list:4
Enter the element of second list:5
Enter the element of second list:6
second list is:  [4, 5, 6]
Union of: [2, 3, 4, 5] and  [4, 5, 6] is:  [2, 3, 4, 5, 6]
```

The following program demonstrates how to find the intersection of two lists and display the result in python.

```l1 = list()
l2 = list()
l3 = list()

n1 = int(input("Enter the number of elements in first list:"))
for i in range(n1):
ele = int(input("Enter the element of first list:"))
l1.append(ele)

print("First list is: ",l1)

n2 = int(input("Enter the number of elements in second list:"))
for i in range(n2):
ele = int(input("Enter the element of second list:"))
l2.append(ele)

print("second list is: ",l2)

for ele in l1:
if ele in l2:
l3.append(ele)

print ("Intersection of ", l1, "and ", l2, "is", l3)
```

### Output

```Enter the number of elements in first list:4
Enter the element of first list:2
Enter the element of first list:3
Enter the element of first list:4
Enter the element of first list:5
First list is:  [2, 3, 4, 5]
Enter the number of elements in second list:3
Enter the element of second list:4
Enter the element of second list:5
Enter the element of second list:6
second list is:  [4, 5, 6]
Intersection of  [2, 3, 4, 5] and  [4, 5, 6] is [4, 5]
```

The following program demonstrates how to find the union and intersection of two lists in one program and display the result in python.

```l1 = list()
l2 = list()
l3 = list()
l4 = list()

n1 = int(input("Enter the number of elements in first list:"))
for i in range(n1):
ele = int(input("Enter the element of first list:"))
l1.append(ele)

print("First list is: ",l1)

n2 = int(input("Enter the number of elements in second list:"))
for i in range(n2):
ele = int(input("Enter the element of second list:"))
l2.append(ele)

print("second list is: ",l2)

l3.extend(l1)
for ele in l2:
if ele in l3:
continue
l3.append(ele)

print("Union of:", l1, "and ", l2, "is: ",l3)

for ele in l1:
if ele in l2:
l4.append(ele)

print ("Intersection of ", l1, "and ", l2, "is", l4)
```

### Output

```Enter the number of elements in first list:4
Enter the element of first list:2
Enter the element of first list:3
Enter the element of first list:4
Enter the element of first list:5
First list is:  [2, 3, 4, 5]
Enter the number of elements in second list:3
Enter the element of second list:4
Enter the element of second list:5
Enter the element of second list:6
second list is:  [4, 5, 6]
Union of: [2, 3, 4, 5] and  [4, 5, 6] is:  [2, 3, 4, 5, 6]
Intersection of  [2, 3, 4, 5] and  [4, 5, 6] is [4, 5]
```

### Summary:

This tutorial discusses the Python program to find the union and intersection of a list. If you like the program please share it with your friends and share your opinion in the comment box. Like the Facebook page for regular updates and YouTube channel for video tutorials.