jack arnold
jack arnold

Reputation: 55

best way to store a list in python?

so basically, I am creating a program where I grab a list of subreddits from Reddit, and am currently storing them in a txt file. however, I dont know how to count duplicates. So if I run it and there is a name that is already in the file, how do i "add 1" to the value? so it could be like : subone : 1 subtwo : 3 subthree: 2

and so on...

here is what i have:

class Isduplicate:
def read(self):
    f = open(r'C:\Users\jacka\OneDrive\Documents\outputs.txt', "r")
    contents = f.read()
    return contents


while counter < len(elem):
    e = str(elem[counter].get_attribute("href"))
    e = e.replace("https://www.reddit.com/r/", "")
    e = e[:-1]

    if e in Is.read():
        text_file.write("duplicate found")
    else:
        text_file.write(e + "\n")


print(e)
counter = counter +2

any general advice is also appreciated!

Upvotes: 1

Views: 554

Answers (1)

Richard Boone
Richard Boone

Reputation: 27

You don't want to use a list for this type of operation. You want to use a dictionary. Make your key the subreddit name and the value the number of mentions of the subreddit. This would best be run by building the dictionary from the file and then rewriting the file each time you run it.

Upvotes: 1

Related Questions