[Tutor] set as iterable

Alan Gauld alan.gauld at yahoo.co.uk
Mon Sep 28 17:56:45 EDT 2020


On 28/09/2020 14:29, Manprit Singh wrote:

> library that accept an iterable as an argument. Some of the functions are:
> all , any, filter, map, max, min, sum, list, tuple, and so on, i will not
> include functions like enumerate since set is an unordered collection . Can
> I pass a set as an argument to these functions in place of an iterable ?

The fastest and surest way to find out is to try it at the
interpreter, it cannot lie. That's why it is there.

And of course you can use it with enumerate() too, why would you not
want that? Knowing the number of iterations you have performed is a
useful  piece of knowledge regardless of the order of items retrieved....

>>> s = {1,2,3}
>>> type(s)
<class 'set'>
>>> for n,x in enumerate(s): print(n,x)

0 1
1 2
2 3
>>>

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list