mixing set and list operations

Tim jtim.arnold at gmail.com
Thu Apr 30 19:47:00 CEST 2015


On Thursday, April 30, 2015 at 1:05:05 PM UTC-4, Ben Finney wrote:
> Tim writes:
> > You can use 'extend' to add set elements to a list and use 'update' to
> > add list elements to a set.
> 
> And you can use both of those methods to add items from a file::
> 
>     >>> foo = ['one', 'two']
>     >>> bar = open('/usr/share/common-licenses/GPL-3')
>     >>> foo.extend(bar)
>     >>> foo
>     ['one', 'two', '                    GNU GENERAL PUBLIC LICENSE\n',
>      ' Version 3, 29 June 2007\n', '\n',
>      ...
> 
> You have merely discovered that 'list.extend' and 'set.update' accept an
> iterable <URL:https://wiki.python.org/moin/Iterator>.
> 
> Sets and lists and files and many other collections are all iterables,
> so any of them can be passed to a function that accepts an iterable.

Thanks for the answers, that makes perfect sense. 
It sure is useful too.
--Tim



More information about the Python-list mailing list