Python removes duplicated items in list

This is a pretty common operation. In python, you just use one line to do this.
See below:

>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]

Of course, you can also use the minus operator to obtain the different items of two list.

0 comments

Popular Posts

无觅相关文章插件,迅速提升网站流量