A Simple Guide to Flatten Python List for Beginners – NLTK Tutorial

By | September 18, 2019

Flatten a python list is very useful to remove duplicated elements in python, which is can help us to build a word dictionary in nlp. In this tutorial, we will introduce you on how to flatten a multi-demensional python list.

If you are using numpy, you also can use flatten() function to flatten an array.

Flatten a Matrix in Numpy for Beginners – Numpy Tutorial

Here we will use pure python code to flatten a multi-demensional list.

Create a multi-demensinal list

grams = [['#i#'], ['lik', 'ike'], ['wri', 'rit', 'itt', 'tti', 'tin', 'ing'], ['#.#']]

Flatten python list

dict =[ n for w in grams for n in w]

the flatten result is:

['#i#', 'lik', 'ike', 'wri', 'rit', 'itt', 'tti', 'tin', 'ing', '#.#']

Remove dumplicated elements

dictionary = set(dict)

Leave a Reply

Your email address will not be published. Required fields are marked *