Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89636 > unrolled thread
| Started by | Tim <jtim.arnold@gmail.com> |
|---|---|
| First post | 2015-04-30 09:07 -0700 |
| Last post | 2015-04-30 10:47 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Tim <jtim.arnold@gmail.com> |
|---|---|
| Date | 2015-04-30 09:07 -0700 |
| Subject | mixing set and list operations |
| Message-ID | <7f1dc7d2-2f88-4b1e-b3ae-bb67ae7e0028@googlegroups.com> |
I noticed this today, using Python2.7 or 3.4, and wondered if it is implementation dependent: You can use 'extend' to add set elements to a list and use 'update' to add list elements to a set. >>> m = ['one', 'two'] >>> p = set(['three', 'four']) >>> m.extend(p) >>> m ['one', 'two', 'four', 'three'] >>> m = ['one', 'two'] >>> p = set(['three', 'four']) >>> p.update(m) >>> p set(['four', 'three', 'two', 'one']) Useful if you don't care about ordering. Not sure if it's dangerous. thanks, --Tim
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-04-30 10:21 -0600 |
| Message-ID | <mailman.131.1430410904.3680.python-list@python.org> |
| In reply to | #89636 |
On Thu, Apr 30, 2015 at 10:07 AM, Tim <jtim.arnold@gmail.com> wrote: > I noticed this today, using Python2.7 or 3.4, and wondered if it is implementation dependent: > > You can use 'extend' to add set elements to a list and use 'update' to add list elements to a set. It's not implementation dependent. Both methods are documented as accepting arbitrary iterables. The same is also true for the other foo_update set methods (and is generally true of built-ins). It is *not* true for the operator versions of the set methods, however (|, -, &, ^). It's also true for dict.update, except that in this case if an iterable is passed instead of a map, then each element of the iterable must be a 2-element iterable.
[toc] | [prev] | [next] | [standalone]
| From | Carl Meyer <carl@oddbird.net> |
|---|---|
| Date | 2015-04-30 10:16 -0600 |
| Message-ID | <mailman.132.1430412418.3680.python-list@python.org> |
| In reply to | #89636 |
[Multipart message — attachments visible in raw view] — view raw
Hi Tim, On 04/30/2015 10:07 AM, Tim wrote: > I noticed this today, using Python2.7 or 3.4, and wondered if it is implementation dependent: > > You can use 'extend' to add set elements to a list and use 'update' to add list elements to a set. > >>>> m = ['one', 'two'] >>>> p = set(['three', 'four']) >>>> m.extend(p) >>>> m > ['one', 'two', 'four', 'three'] > >>>> m = ['one', 'two'] >>>> p = set(['three', 'four']) >>>> p.update(m) >>>> p > set(['four', 'three', 'two', 'one']) > > > Useful if you don't care about ordering. Not sure if it's dangerous. I don't think this is surprising, nor implementation dependent, nor dangerous. Lists have an `extend()` method, sets have an `update()` method. Both of these methods take any iterable as input, they don't needlessly constrain the input to be of the same type as the base object. That's the Pythonic way to do it; I'd be surprised if it didn't work. Carl
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2015-05-01 03:04 +1000 |
| Message-ID | <mailman.134.1430413495.3680.python-list@python.org> |
| In reply to | #89636 |
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
[toc] | [prev] | [next] | [standalone]
| From | Tim <jtim.arnold@gmail.com> |
|---|---|
| Date | 2015-04-30 10:47 -0700 |
| Message-ID | <efe21e2e-f52a-4bd9-8201-68a6e64c23a1@googlegroups.com> |
| In reply to | #89644 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web