How to get the only element of a set? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get the only element of a set?

I write a program to solve sudoku. I store all the optional assinments for a specific cell, e.g. t = {2,4,7}. While processing the data, I remove 2 and 7 from t. Then the desired condition len(t) = 1 is True. How can I get the only element from t, viz. 4? I tried .pop method, but it changes t. What can I do? Maybe a "for i in t" would help?

14th Feb 2017, 8:44 PM
Hashahar Ha'oleh
6 Answers
14th Feb 2017, 9:39 PM
Mario L.
Mario L. - avatar
0
you can't just take it? if len(t)==1: cell=t[0]
14th Feb 2017, 9:03 PM
LordHill
LordHill - avatar
0
No, a set is not indexed.
14th Feb 2017, 9:04 PM
Hashahar Ha'oleh
0
yup, sorry I missed set. thought you had a list.. a for loop would work t={1} for i in t: break print(i)
14th Feb 2017, 9:15 PM
LordHill
LordHill - avatar
14th Feb 2017, 9:44 PM
Elite
Elite - avatar
0
Thanks all. I used tuple unpacking: a, = b when b is the one-element set.
15th Feb 2017, 1:40 PM
Hashahar Ha'oleh