jeremybcenteno
jeremybcenteno

Reputation: 139

Inquiry about printing Python Dictionaries

I have a Python dictionary called top_words.

I wanted to print its contents so I have written this code.

for word, count in top_words.items():

    print(word, count)

It displays something like this.

enter image description here

How can I remove the numbers before each rows?

The top_words dictionary is being generated by this function.

top_word_sequence = 0
top_word_dataset = {}

conn = sqlite3.connect('app.db')
selector = conn.cursor()

query = "SELECT word, count FROM top_words WHERE brand_id = ? ORDER BY count desc LIMIT 10"
selector.execute(query,(brand_id,))

top_words = selector.fetchall()

for single_word in top_words:

    top_word_sequence += 1

    word = single_word[0]
    count = single_word[1]

    top_word_dataset[top_word_sequence] = { 'word' : word, \
                                            'count' : count }

    return top_word_dataset

Upvotes: 0

Views: 125

Answers (2)

Will_of_fire
Will_of_fire

Reputation: 1189

Seems like you just want the values from the dictionary top_words.

for key, value in top_words.items():
    print(value)

Or, you can do it like this:

for value in top_words.values():
    print(value)

Upvotes: 0

user6261418
user6261418

Reputation:

Use this:

for word, count in top_words.items():
    print(word, count)

Note: Using print(word, count) adds some other formatting to the output. Hope this helps.

Upvotes: 1

Related Questions