2018-06-13 14:25:30 8 Comments
I have three lists X, Y, Z as follows:
X: [1, 1, 2, 3, 4, 5, 5, 5]
Y: [3, 3, 2, 6, 7, 1, 1, 2]
Z: [0, 0, 1, 1, 2, 3, 3, 4]
I am trying to remove both duplicated set of values at the same index of the lists get a reduced list as follows, all three list will always have the same length initially and at the end as well:
X: [2, 3, 4, 5]
Y: [2, 6, 7, 2]
Z: [1, 1, 2, 4]
I tried using the zip(X, Y, Z) function but I can't index it and the dict.fromkeys only removes one of the duplicates and leaves the other in the new list. I want to be able to remove both.
Any help is appreciated!
Related Questions
Sponsored Content
9 Answered Questions
[SOLVED] Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?
- 2015-05-06 15:32:43
- Rick Teachey
- 134523 View
- 1570 Score
- 9 Answer
- Tags: python performance python-3.x range python-internals
26 Answered Questions
[SOLVED] Does Python have a ternary conditional operator?
- 2008-12-27 08:32:18
- Devoted
- 1596325 View
- 4958 Score
- 26 Answer
- Tags: python operators ternary-operator conditional-operator python-2.5
6 Answered Questions
36 Answered Questions
40 Answered Questions
[SOLVED] How do I check whether a file exists without exceptions?
- 2008-09-17 12:55:00
- spence91
- 3362471 View
- 4802 Score
- 40 Answer
- Tags: python file file-exists
26 Answered Questions
[SOLVED] Difference between append vs. extend list methods in Python
- 2008-10-31 05:55:36
- Claudiu
- 2446694 View
- 2968 Score
- 26 Answer
- Tags: python list data-structures append extend
28 Answered Questions
39 Answered Questions
[SOLVED] How to make a flat list out of list of lists?
- 2009-06-04 20:30:05
- Emma
- 1393521 View
- 2418 Score
- 39 Answer
- Tags: python list multidimensional-array flatten
30 Answered Questions
[SOLVED] How to concatenate two lists in Python?
- 2009-11-12 07:04:09
- y2k
- 1698463 View
- 1902 Score
- 30 Answer
- Tags: python list concatenation
15 Answered Questions
[SOLVED] What are metaclasses in Python?
- 2008-09-19 06:10:46
- e-satis
- 670062 View
- 4950 Score
- 15 Answer
- Tags: python oop metaclass python-datamodel
4 comments
@Laurent H. 2018-06-13 15:20:48
Here is my solution without any import, but still short and easily readable:
@jpp 2018-06-13 14:31:02
Using
collections.Counter
andzip
, you can count unique triplets.Then remove duplicates via a generator comprehension.
Note if ordering is a requirement and you are not using Python 3.6+, you can create an "OrderedCounter" instead by subclassing
collections.OrderedDict
.@jpp 2018-06-13 14:32:40
Downvoter care to comment?
@Jared Goguen 2018-06-13 14:39:54
correct me if I'm wrong, but this does not guarantee that the order of the triplets will be preserved - albeit, the triplets themselves will be. I'm not the down-voter, just an observation
@jpp 2018-06-13 14:41:43
@JaredGoguen, Good point: in Python 3.6+ it would, not in <3.6. You could construct an OrderedCounter (subclassing
collections.OrderedDict
), but not sure if order is a requirement and OP's Python version.@Jared Goguen 2018-06-13 14:42:59
that is a side effect of the CPython implementation and is not, in general, guaranteed in 3.6. It will however be guaranteed in 3.7
@Jared Goguen 2018-06-13 14:44:07
of course, we don't even know if preserving order is required by the OP
@shanmuga 2018-06-13 14:38:15
Not the best possible approach
@koPytok 2018-06-13 14:31:56
It's convenient to use pandas library for the task. Just create dataframe using the lists and apply
df.drop_duplicates
withkeep=False
(means remove all duplicated rows):