Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make tuple of sets in Python

I want to put two sets in a tuple in Python.

set1 = set(1, 2, 3, 4)
set2 = set(5, 6, 7)

What I’ve tried:

result = tuple(set1, set2)
# got error "TypeError: tuple expected at most 1 argument, got 2"

Desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

({1, 2, 3, 4}, {5, 6, 7})

>Solution :

A tuple literal is just values separated by commas.

set1 = {1,2,3,4}
set2 = {5,6,7}
result = set1, set2

or if you find it clearer

result = (set1, set2)

The tuple(x) form is useful when you have an iterable sequence x that you want to transform to a tuple.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading