Skip to content
Advertisement

How to create a new list of all the words separating by each word and each line?

I have a text file as :

sample.txt

JavaScript

What I’ve tried is

JavaScript

This results

JavaScript

I want the result as:

JavaScript

Advertisement

Answer

After opening the file, you can use a list comprehension to iterate over lines, and for each line str.split on whitespace to get tokens for each sublist.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement