# Filtering words from a list in python



## bharatk (Jan 27, 2010)

Hello

I have generated a list of words & their occurrences.
Now, how to I filter them based to the starting alphabet. I.e. all words starting with 'a' should be filtered from thos list & put into 1 list along with their occurrences, similarly b,c & d.

This is the list I've generated.It shows the words & the number of occurrences of the words.


```
[('a', 4574), ('abandon', 3), ('abandoned', 8), ('abandoning', 1), ('abide', 1), ('ability', 2), ('abjectly', 1), ('abjure', 1), ('able', 45), ('abnormal', 4), ('abnormality', 1), ('abolition', 1), ('about', 487), ('above', 27), ('babbled', 1), ('baby', 1), ('back', 131), ('backed', 1), ('backing', 1), ('backs', 1), ('backwards', 2), ('bad', 25), ('bade', 2), ('badly', 10), ('bags', 2), ('baked', 1), ('baking', 1), ('balance', 1), ('balancing', 1), ('balcony', 1), ('bald', 1), ('ball', 3), ('balloon', 3), ('balls', 1), ('bandage', 1), ('banging', 2), ('bank', 9), ('bankrupt', 1), ('banks', 4), ('banquets', 1), ('bantering', 1), ('bar', 3), ('bare', 9), ('bared', 1), ('barefoot', 1), ('bareheaded', 1), ('barely', 1), ('bargain', 1), ('bargained', 1), ('baring', 1), ('barking', 1), ('barracks', 1), ('barred', 1), ('barrel', 1), ('barricade', 1), ('barrier', 1), ('barriers', 2), ('barter', 2), ('base', 6), ('based', 2),  ('cease', 4), ('ceased', 13), ('ceasing', 1), ('ceiling', 1), ('ceilings', 1), ('celebrate', 1), ('cellar', 1), ('censorship', 1), ('censure', 1), ('centered', 1), ('central', 1), ('centre', 1), ('centres', 1), ('century', 2), ('ceremoniously', 1), ('ceremony', 3), ('certain', 66), ('certainly', 63), ('certainty', 2), ('certificate', 7), ('cesspool', 1), ('chain', 3), ('chair', 26), ('chairs', 5), ('chalk', 1), ('challenge', 1), ('challenged', 1), ('challenging', 2), ('chamois', 2), ('champagne', 6), ('champion', 1), ('chance', 16), ('chanced', 5), ('change', 21), ('changed', 19), ('changes', 1), ('changing', 3), ('chantant', 1), ('chanted', 1), ('chapter', 40), ('character', 15), ('characteristic', 4), ('characteristics', 2), ('charge', 9), ('charged', 1), ('charges', 1), ('charitable', 2), ('charities', 1), ('charm', 1), ('charmed', 1), ('charming', 4), ('charwoman', 1), ('chase', 1),  ('defiled', 1), ('definite', 9), ('definitely', 1), ('deformed', 1), ('degradation', 1), ('degrade', 1), ('degraded', 1), ('degrading', 2), ('degree', 2), ('degrees', 7), ('deigned', 1), ('deigning', 1), ('dejected', 3), ('dejectedly', 2), ('delay', 2), ('delayed', 1), ('deletions', 1), ('deliberate', 2), ('deliberately', 3), ('deliberating', 1), ('deliberation', 1), ('delicacy', 2), ('delicate', 5), ('delicately', 2), ('delight', 1), ('delighted', 10), ('delightful', 3), ('delirious', 7), ('delirium', 9), ('deliverance', 2), ('delivered', 2), ('delusions', 1), ('demand', 3), ('demanded', 1), ('demands', 3), ('demonstrations', 1), ('demoralisation', 1), ('den', 2), ('denied', 1), ('dens', 1), ('dense', 1), ('denser', 1), ('deny', 10), ('denying', 2), ('departed', 1), ('departing', 1), ('department', 3), ('departure', 2), ('depend', 4), ('depended', 4), ('dependent', 4), ('depending', 1), ('depends', 8), ('depose', 1), ('depositing', 1), ('deposition', 1), ('depraved', 2), ('depravity', 2), ('depressed', 2), ('depression', 1), ('deprived', 1), ('depriving', 1), ('depth', 1), ('depths', 2)]
```
Thank you :grin:


----------



## Stu_computer (Jul 7, 2005)

re: tsf rules


> EDUCATIONAL ASSISTANCE
> While we are happy to promote the education of users, we feel it is inappropriate to circumvent the learning process by directly assisting with assignments and projects of an educational nature. Direct answers to school or college assignments are not allowed and will not be given. However, if a user is having trouble comprehending a certain topic, the members and staff at TSF will be happy to provide assistance with understanding the topic.


post the code segment you have written and identify the troubled part.


----------



## bharatk (Jan 27, 2010)

ok here is the code I wrote. The output of this is only given in my previous post.


```
import operator
f = open('2554.txt', 'r')
raw = f.read()
f.close()
wordlist = raw.lower().split(None)
wordlist= filter(lambda c: c.isalpha(), wordlist) 
wordfreq={}                                       
for word in wordlist:                              
     wordfreq[word] = wordfreq.get(word, 0) + 1    
print raw[:75]
print sorted(wordfreq.items())
raw_input()
```
This gives a sorted list of all the words & the number of times they occur.
My doubt is how do I only filter the words beginning with a & put them in a dictionary along with their occurrences.
I tried used the filter() but not able to figure out how exactly should I do it.

Thank you.


----------

