GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Union() Function in Set Python - GeeksforGeeks Union() Function in Set PythonLast Updated : 18 Apr, 2026 The set.union() method in Python returns a new set containing all unique elements from two or more sets. It combines the given sets and automatically removes duplicate values. The original sets remain unchanged. ![]() Example: In this example, two sets a and b are combined using union().
Output {1, 2, 3, 4, 5}
Explanation: a.union(b) merges both sets. Duplicate element 3 appears only once. Syntax
ExamplesExample 1: In this example, union on three sets is performed using multiple union() calls and by passing multiple sets directly.
Output {2, 4, 5, 6, 7, 8, 9, 10}
{2, 4, 5, 6, 7, 8, 9, 10}
Explanation: a.union(b).union(c) merges sets step by step and a.union(b, c) merges all sets at once. Example 2: Here, | operator is used as a shortcut for union operation on sets.
Output {2, 4, 5, 6, 7, 8}
{2, 4, 5, 6, 7, 8, 9, 10}
Explanation:
Example 3: In this example, union() is applied on sets of strings.
Output {'cd', 'za', 'ba', 'ab', 'dd', 'dz'}
Explanation:
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |