- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find Union of two or more Lists?
Union operation means, we have to take all the elements from List1 and List 2 and all the elements store in another third list.
List1::[1,2,3] List2::[4,5,6] List3::[1,2,3,4,5,6]
Algorithm
Step 1: Input two lists. Step 2: for union operation we just use + operator.
Example code
# UNION OPERATION A=list() B=list() n=int(input("Enter the size of the List ::")) print("Enter the Element of first list::") for i in range(int(n)): k=int(input("")) A.append(k) print("Enter the Element of second list::") for i in range(int(n)): k=int(input("")) B.append(k) C = A + B print("THE FINAL LIST IS ::>",C)
Output
Enter the size of the List ::4 Enter the Element of first list:: 23 11 22 33 Enter the Element of second list:: 33 22 67 45 THE FINAL LIST IS ::> [23, 11, 22, 33, 33, 22, 67, 45]
Advertisements