From 7f25b4de802868a072b0091ad007dc920ed592f7 Mon Sep 17 00:00:00 2001 From: Rob-Rychs Date: Sun, 1 Apr 2018 01:03:09 -0700 Subject: [PATCH] add has_duplicates and all_unique --- snippets/all_unique.md | 16 ++++++++++++++++ snippets/has_duplicates.md | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 snippets/all_unique.md create mode 100644 snippets/has_duplicates.md diff --git a/snippets/all_unique.md b/snippets/all_unique.md new file mode 100644 index 000000000..020e65f2e --- /dev/null +++ b/snippets/all_unique.md @@ -0,0 +1,16 @@ +### all_unique + +Checks a flat list for all unique values. Returns True if list values are all unique and False if list values aren't all unique. + +This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list. + +``` python +def all_unique(lst): + return len(lst) == len(set(lst)) +``` + +``` python +x = [1,2,3,4,5,6] +all_unique(x) # result +True +``` \ No newline at end of file diff --git a/snippets/has_duplicates.md b/snippets/has_duplicates.md new file mode 100644 index 000000000..2c0f65f5e --- /dev/null +++ b/snippets/has_duplicates.md @@ -0,0 +1,16 @@ +### has_duplicates + +Checks a flat list for duplicate values. Returns True if duplicate values exist and False if values are all unique. + +This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list. + +``` python +def has_duplicates(lst): + return len(lst) != len(set(lst)) +``` + +``` python +x = [1,2,3,4,5,5] +has_duplicates(x) # result +True +```