Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89644

Re: mixing set and list operations

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: mixing set and list operations
Date 2015-05-01 03:04 +1000
References <7f1dc7d2-2f88-4b1e-b3ae-bb67ae7e0028@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.134.1430413495.3680.python-list@python.org> (permalink)

Show all headers | View raw


Tim <jtim.arnold@gmail.com> 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.

-- 
 \          “It's dangerous to be right when the government is wrong.” |
  `\                                   —Francois Marie Arouet Voltaire |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

mixing set and list operations Tim <jtim.arnold@gmail.com> - 2015-04-30 09:07 -0700
  Re: mixing set and list operations Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-30 10:21 -0600
  Re: mixing set and list operations Carl Meyer <carl@oddbird.net> - 2015-04-30 10:16 -0600
  Re: mixing set and list operations Ben Finney <ben+python@benfinney.id.au> - 2015-05-01 03:04 +1000
    Re: mixing set and list operations Tim <jtim.arnold@gmail.com> - 2015-04-30 10:47 -0700

csiph-web