I am trying to save a dictionary to a list, but the dictionary loses its values and becomes a string

    itemsInExistence = []
    item = {}
    item['name'] = input("What do you want the new item to be called? ")
    item['stats'] = int(input("What is its stat? "))
    item['rank'] = int(input("What is its base rank? "))
    item['amount'] = int(input("How many of it are there? "))
    for i in range(item['amount']):

When item is appended to itemsInExistence, it becomes one string. This is a problem, because I need to change the values of item['rank'], item['stats'], and item['amount']. And getting the values by itemsInExistence[num][-num] doesn't work, it won't let me change it from a string to an integer.

1 answer

  • answered 2019-11-08 14:04 c8999c 3f964f64

    Because the code you posted seems to run fine, there's possibly an error in how you retrieve the values back from the list (which is, unfortunately, called "dictionary" in your code)

    make sure you use something along the lines of

    for dict in dictionary_which_is_actually_a_list:
      for k, v in dict.items():

    because the structure is [{},{},{}...]

    dict will be {}

    and k,v will be 'word', 'word user typed', 'meaning', 'meaning user typed', etc...

    Alternatively, if you dont ever intended to use this list which contains multiple dictionaries, just use

    dict.update({"word": "word user typed"})

    when initially generating the dict or when changing it later - making the variable "dictionary" & the functionality of dictionary.append unnecessary