summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tablib/core.py7
-rwxr-xr-xtest_tablib.py19
2 files changed, 26 insertions, 0 deletions
diff --git a/tablib/core.py b/tablib/core.py
index f8e2df7..069646c 100644
--- a/tablib/core.py
+++ b/tablib/core.py
@@ -934,6 +934,13 @@ class Dataset(object):
return _dset
+ def unique(self):
+ """Removes all duplicate rows from the :class:`Dataset` object
+ while maintaining the original order."""
+ seen = set()
+ self._data[:] = [row for row in self._data if not (tuple(row) in seen or seen.add(tuple(row)))]
+
+
def wipe(self):
"""Removes all content and headers from the :class:`Dataset` object."""
self._data = list()
diff --git a/test_tablib.py b/test_tablib.py
index ba57170..ad182ab 100755
--- a/test_tablib.py
+++ b/test_tablib.py
@@ -696,6 +696,25 @@ class TablibTestCase(unittest.TestCase):
self.assertEqual(third_row, expected_third)
+ def test_unique(self):
+ """Unique Rows."""
+
+ self.founders.append(self.john)
+ self.founders.append(self.george)
+ self.founders.append(self.tom)
+ self.assertEqual(self.founders[0], self.founders[3])
+ self.assertEqual(self.founders[1], self.founders[4])
+ self.assertEqual(self.founders[2], self.founders[5])
+ self.assertEqual(self.founders.height, 6)
+
+ self.founders.unique()
+
+ self.assertEqual(self.founders[0], self.john)
+ self.assertEqual(self.founders[1], self.george)
+ self.assertEqual(self.founders[2], self.tom)
+ self.assertEqual(self.founders.height, 3)
+
+
def test_wipe(self):
"""Purge a dataset."""