Reputation: 954
I have a problem. I want to set all the values from my dictionary that are connected with the ["key1"]
to lowercase. I started to create a test dictionary:
# Define test devices
item1 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
item2 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
collection = []
collection.append(item1)
collection.append(item2)
After that, I started by tring to set every value to lowercase like this:
for item in collection:
item = dict((k, v.lower()) for k,v in item.items())
But after that, I printed the collection, but nothing changed. Why are all my values not lowercase and how can I set it for a specific key?
Upvotes: 0
Views: 1898
Reputation: 451
You can update the list using the below code:
for item in collection:
item.update(dict((k, v.lower()) for k,v in item.items() if k=='key1'))
print(collection)
Output
[{'key1': 'value1', 'key2': 'VALUE2'}, {'key1': 'value1', 'key2': 'VALUE2'}]
Upvotes: 0
Reputation: 54
There is just a little problem of misconception in how updating a dictionary works. You are trying to use the item coming out of the for, while this is just a copy of the original. You could just use the key to refer to the item in the originnal dictionnary and update it:
# Define test devices
item1 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
item2 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
collection = []
collection.append(item1)
collection.append(item2)
for item in collection:
for k,v in item.items():
if k == "key1":
item[k] = v.lower()
print(collection)
Upvotes: 0
Reputation:
help your self with this approach
item1 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
for k, v in item1.items():
if k == 'key1':
item1.update({k:v.lower()})
print(item1)
output
{'key1': 'value1', 'key2': 'VALUE2'}
Upvotes: 1
Reputation: 223
Here added an extra piece of code
for i in collection:
i["key1"] = i["key1"].lower()
Here is the file
item1 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
item2 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
collection = []
collection.append(item1)
collection.append(item2)
print("Before operation",collection)
for i in collection:
i["key1"] = i["key1"].lower()
print("After Operation",collection)
Upvotes: 0
Reputation: 300
Here is a working example
# Define test devices
item1 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
item2 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
collection = (item1,item2)
for item in collection:
for k,v in item.items():
item[k] = v.lower()
Upvotes: 0
Reputation: 64
for item in collection:
for key in item:
if key=="key1":
item[key]=item[key].lower()
Why?
In python, strings are immutable. When you use string.lower()
, you create a copy of the original string and turn all the characters into lowercases. Hence you need the key to point to the new string. Unless you reassign the new string, the old string would not be replaced.
Upvotes: 0
Reputation: 719
item1 = {
"key1": "VALUE1",
"key2": "VALUE2"
}
for key, value in item1.items():
item1[key]=value.lower()
print(item1)
output {'key1': 'value1', 'key2': 'value2'}
Upvotes: 0
Reputation: 3036
When using for loop with dictionary, you iterate through the keys present in it. All you need to do is assign to the corresponding key using dictionary[key] = ...
. The dictionary[key]
in the right-hand side fetches the value associated with the key upon which you may call the lower()
function.
This will fix the issue:
for key in dictionary:
dictionary[key] = dictionary[key].lower()
Upvotes: 1