user2296226
user2296226

Reputation: 75

How do I append items to a list inside a for loop in python?

What I want to do is take names from namelist, compare those to names in banklist, and if there is an item in banklist which looks a lot like an item in namelist, I want to append that item to a closematchlist. The goal of this is to find items that occur in both lists, even if there is a spelling error in namelist. When I print(closematch), it works like intended: the close matches in banklist get found and printed. However, when I try to append those items to a list, the result of print(closematchlist) is [].

for name in namelist:
    closematch = difflib.get_close_matches(name, banklist, 1, 0.8)
    closematchlist = list()
    closematchlist.append(closematch)
    print(closematch)
print(closematchlist)```

Upvotes: 0

Views: 140

Answers (1)

Lennart Regebro
Lennart Regebro

Reputation: 172367

difflib.get_close_matches() is a list of close matches. You don't need to copy it to a new list.

Upvotes: 3

Related Questions