Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89641
| Date | 2015-04-30 10:16 -0600 |
|---|---|
| From | Carl Meyer <carl@oddbird.net> |
| Subject | Re: mixing set and list operations |
| References | <7f1dc7d2-2f88-4b1e-b3ae-bb67ae7e0028@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.132.1430412418.3680.python-list@python.org> (permalink) |
[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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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