I have this dictionary in python:
example = {
"foo": ["1"],
"bar": ["1","2"]
}
How can i transform efficiently and using Python the variable example to:
example = {
"foo": "1",
"bar": ["1","2"]
}
?
I have this dictionary in python:
example = {
"foo": ["1"],
"bar": ["1","2"]
}
How can i transform efficiently and using Python the variable example to:
example = {
"foo": "1",
"bar": ["1","2"]
}
?
Assuming the dict is not nesting, you can iterate over the dict to modify it: (Python 3, see here for difference)
for key, value in example.items():
if isinstance(value, list) and len(value) == 1:
example[key] = value[0]