Find amount of times that the chosen letters appear
Question: I have a script where im reading from a file and taking the first two
characters from each word in the line, what i am trying to do is find out
which two letters shows up the most often, do i have to convert my output here
into a list and do it that way ?

here is what i have

    
    
    #!/usr/bin/python
    
    import string
    import re
    import random
    import sys
    
    
    file = raw_input("Enter path to filename :")
    
    text_file= open(file,'r')
    data=text_file.readlines()
    firsttwo =[]
    lines = []
    
    def first2():
        for line in data:   
        firsttwo = line[:2]
        print firsttwo
    
    print first2()
    


Answer: You can use `Counter` that counts the appearances of a item in a list.

    
    
    from collections import Counter
    
    text_file= open("C:/test.txt",'r')
    firsttwo = [line[:2] for line in text_file.readlines()]
    
    print Counter(firsttwo)
    

If the content of `test.txt` is:

    
    
    first line
    second line
    second line
    third line
    

The output of the code provied would be:

    
    
    Counter({'se': 2, 'fi': 1, 'th': 1})
    

If you want to convert this output to a list, you can do:

    
    
    list(Counter(firsttwo).items())
    

This outputs:

    
    
    [('fi', 1), ('th', 1), ('se', 2)]
    

**EDIT (without collections):**

    
    
    text_file= open("C:/test.txt",'r')
    firsttwo = [line[:2] for line in text_file.readlines()]
    l_items = set(firsttwo) 
    l_counts = [(firsttwo.count(x), x) for x in set(firsttwo)]
    l_counts.sort(reverse=True)
    print l_counts[0][1]
    

