[Tutor] General design question

Kent Johnson kent37 at tds.net
Wed Jul 2 12:55:28 CEST 2008


On Wed, Jul 2, 2008 at 6:31 AM, Daniele <d.conca at gmail.com> wrote:
>>> If I have a set of numbers, or strings etc. which have been generated and I then want to do something with them, for example a sum function call.  Is the best way to put those items in a list, or similar container, before applying the function.
>>
>> Not only "best" but "necessary". the sum () builtin takes an
>> iterator -- often a list, but needn't be -- of numbers.
>
> I'm a total newbie, but wouldn't it be possible to use a set (from
> which I think you can get an iterator)?

Sets and lists are not interchangeable. A set removes duplicates, is
not ordered and has a fast membership test; a list preserves
duplicates, is ordered and is relatively slow for membership testing.
Members of a set must be hashable, so you can't put lists and dicts
into a set; members of a list can be any object.

Yes, you can get an iterator from a set (and a list).

Kent


More information about the Tutor mailing list