mixing set and list operations

Tim jtim.arnold at gmail.com
Thu Apr 30 18:07:25 CEST 2015


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




More information about the Python-list mailing list