
- Change value in list of dictionaries python how to#
- Change value in list of dictionaries python code#
Change value in list of dictionaries python how to#
Same output though, no change but no error.ĭoes anyone know how to replace a specific part of a dictionary value with a new value? The HTML in dataDict must not change. I made a copy of the dataDict` list and I cast the whole list as a string and even that didn't work.
Change value in list of dictionaries python code#
return()' function which did make the function change font in my code editor, so I now believe that that function can only work for strings. There is also an if statement that'll do the same should that current number in the loop also come with a. No error is raised but 'dataDict' remains unchanged. Please note that each number in that line of code (excluding the final '1') changes in the loop I have/ I created a whole loop for the following code but it just changes the "stringX" number - this is what I'm doing with each number in said loop:ĭataDict.replace(htmlNew, response, 1) Please note that these n and x variables don't exist in my code, I just used them for explanation purposes./ What my plan was, was to use python's variable.replace(n, x, y) function, where n = htmlNew" and x = response ( y will just be 1). To select rows with a specified column value: myvalue 3 results mydataframe.loc mydataframe 'columnname' myvalue To select rows that do not match a specified column value: myvalue 3 results mydataframe. The first one, I figured, shouldn't be a problem since it's just a regular string with no HTML, but even that one isn't being changed. The Solution This can be achieved using the DataFrame’s loc property. What I want to do is replace the values of each 'Sentence' key in the dataDict list, but only at specific places as to not remove the original HTML tags. Each value is stored as "responseX" where X is the same number that the value had in the htmlNew dictionary. This dictionary is then passed through a local AI that rephrases each value from the htmlNew dictionary and stores the new output into a dictionary called response. This HTML algorithm also separates them, and stores one as "stringN" and the other as "stringN.5". I should also mention that some values, such as the second 'Sentence' in the original dataDict list, have 2 separate bodies of text in their values. Those values are then put through an algorithm I made that strips them of their HTML tags (if they have any) and are then stored in another dictionary (called htmlNew) with the same "stringX" key system.

The numbers are in ascending order and they're simply just a way to tell the difference between "string1" and "string2". In the program I've written, the value of each 'Sentence' key is copied into a dictionary (called first), each with a new key: "stringX" where 'X' is replaced by whatever number it's assigned when it's put in. The third key also has a small amount of HTML code at the beginning of it's text, but it's pretty insignificant. The first `'Sentence' Key contains a string value, but the second key has a line of HTML code which has 2 pieces of text inside it. I've got this list of Dictionaries: dataDict = [Īs you'll be able to see there are 3 'Sentence' Keys, 1 for each dictionary in this list. I also don't get any errors, the value just doesn't change. I'm trying to change the value of a dictionary in my program, but nothing seems to work.
