So briefly, I’m on a python ZTM course, by the one and only Andrei Neagoi, in the Functional programming section precisely, there was this exercise that, required creating a list, that contains the duplicates from another list, instead of using the following for loop:
some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
duplicates = []
for dup in some_list:
if some_list.count(dup):
if dup not in duplicates:
duplicates.append(dup)
use list or set comprehensions, so here is my answer:
a_list=[char for char in "foobar hello world taha yasser adnan" if char != ' ']
duplicates=[item for item in {dup for dup in a_list if a_list.count(dup) > 1}]
But Mr.Andrei used
duplicates = list(set([x for x in some_list if some_list.count(x) > 1]))
I’ve even added this line and was so happy when it worked out
count={n:a_list.count(n) for n in duplicates}
my questions is, when defining duplicates, was my method confusing or hard to read, thanks for every response in advance
Nothing there is no issue