# Fetching and modifying CNN headlines

This is a project I came up just as an exercise with to familiarize myself with Python syntax and data types. I am just learning how to code and would like to avoid establishing bad habits. On the other hand, I wanted this program to function so when I found something that worked, I left it as is, even if I suspected that there might be a better way. Any suggestions on how to improve the code would be greatly appreciated.

import feedparser
import nltk
import random

# using words from original headlines and mimicking the syntax of original headlines

#scrape titles into scrptitles list

scrptitles=[]
for entry in data.entries:
data = entry.title
scrptitles.append(data)

#tokenize and tag

toks = [(nltk.pos_tag(nltk.word_tokenize(title))) for title in scrptitles]

# create list of dictionaries from list of tuples

d_list = [dict(toks[1]) for toks[1] in toks]

# remove unwanted characters from d_list ['',"''",'>','.',',','']

nonetype_list = ['',"''","'",'>',',','.','|','(',')','-   ','_','=','+','<','@','#','\$',"%",'^','*','[',']','{','}',":",";"]

for dicts in d_list:
for item in nonetype_list:
if item in dicts:
del dicts[item]

# create list of tags perserving headline structure

tags_list =  [dict.values() for dict in d_list]

# get a clean version of the tags list

tags_list = [[element for element in dict_values] for dict_values in tags_list]

# reverse dictionaries inside of list of dictionaries
# inv_map = {TAG: [list of words]}

inv_map = {}
for dct in d_list:
for k, v in dct.items():
inv_map[v] = inv_map.get(v,[])
inv_map[v].append(k)

# grab a headline_pattern, choose random words from dictionary values,


• This looks like a bug. Is it? for entry in data.entries: ; data = entry.title ; scrptitles.append(data) (the scrptitles is not indented in your code) – Austin Hastings Feb 19 at 23:20
• I don't think [dict(toks[1]) for toks[1] in toks] does what you think it does. You first two entries of the resulting list are actually the same. If you want to make every entry into a dictionary, use [dict(t) for t in toks] or list(map(dict, toks)). – Graipher Feb 20 at 10:14