In Similarities, I'm told that my functions in helpers.py (lines, sentences, and substrings) should return lists. However, when I try testing my functions, I receive an error telling me that "object 'list' does not have attribute 'replace'." I am directed to an error in compare, where main takes the list I returned and then takes the individual strings in a variable called "match".

When I test the outputs of my function, it looks like I return the list correctly. However, compare seems to put the entire list into "match", instead of just one string at a time.

Here is my code for lines:

def lines(a, b):
"""Return lines in both a and b"""

alist = a.splitlines()
blist = b.splitlines()

results = list(set(alist) & set(blist))


return [results]

And here is the relevant part of compare:

# Compare files
if args["lines"]:
    matches = lines(file1, file2)
elif args["sentences"]:
    matches = sentences(file1, file2)
elif args["substrings"]:
    matches = substrings(file1, file2, args["substrings"])

# Output matches, sorted from longest to shortest, with line endings escaped
for match in sorted(matches, key=len, reverse=True):
    print(match.replace("\n", "\\n").replace("\r", "\\r"))

2 Answers 2


Not knowing this problem (regular CS50 without x?), I can tell you what the code does.

results = list(set(alist) & set(blist))

turns the intersection of those two sets into a list.

return [results]

returns a new list with one element, the list of strings.

Your later code lets me assume you want a list of strings, not a list containing exactly one list of strings.

So use

return results



You might consider a one line solution using set() splitlines() and intersection().

Some psudo code would look like this:

return the set of the intersection of alist splitlines and blist splitlines.

  • Do not make confusion!!! return the list not the set
    – grsh
    Apr 27, 2018 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .