MoSiraj
MoSiraj

Reputation: 11

List comprehension with .append() produces [None] result

I'm trying to write a code in Python 3.x using list comprehension. My code should print letters out of a list and remove duplication.

print(list(set(([letter_list.append(letter) for word in word_list for letter in word]))))

The code runs with no traceback errors but the output is [None]

Upvotes: 1

Views: 88

Answers (1)

sammy
sammy

Reputation: 867

The append method modifies an (existing) list in place and returns None. A list comprehension creates a new list by itself, so you don't need appending here. Try this:

print(list(set([letter for word in word_list for letter in word])))

Upvotes: 1

Related Questions