Melanie432k Melanie432k - 1 month ago 10
Python Question

Inputting key words and their values on Python

Simple question. I tried to search it up but couldn't find much information about what Im trying to do.
Im trying to create a list with words and integer values and put them in the list together. For example,
keywords:

rain,10
shine,5
python,10
great,1


Well i know how to put the words in a list, but not the values. So I did so far:
(Still using examples)
pyList = {'python', 'great, 'shine', rain}

So basically im trying to input the keywords and their values and store them in a list

Answer

A dictionary might be better than a list.

item_dict = {'rain': 10, 'shine': 5, 'python': 10, 'great': 1}

The following code

print(item_dict['rain'])

will output 10

If you don't care what numbers they have, but just want a different one for each then you can enumerate a list and turn that into a dictionary.

items = ['rain','shine','python','great']
items_dict = dict(enumerate(items))

This will give you a dictionary with the following values

{0: 'rain', 1: 'shine', 2: 'python', 3: 'great'}