'I want to sort this list of email address

So I am trying to sort a list of emails to see how many are sent by each email but I keep getting:

    filename = input("Enter File Name ")
file = open(filename)
lst = list()
for line in file :
    if line.startswith('From: '):
        y = line.split('From: ')[1]
        y.sort()
        print(y)

----> 7 y.sort()

      8        print(y)

AttributeError: 'str' object has no attribute 'sort'



Solution 1:[1]

You need to append each line to your list, then sort the list. y.sort() is trying to sort an individual line; the error happens because y is a string, and they don't have a sort() method.

filename = input("Enter File Name ")
file = open(filename)
lst = list()

for line in file:
    if line.startswith('From: '):
        lst.append(line.split('From: ')[1])

lst.sort()
print(lst)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 baileythegreen