I am getting myself all tangled up where in the nesting.
I have a list of python objects that look like this:
notes = [
{'id':1,
'title':'title1',
'text':'bla1 bla1 bla1',
'tags':['tag1a', ' tag1b', ' tag1c']},
{'id':2,
'title':'title2',
'text':'bla2 bla2 bla2',
'tags':[' tag2a', ' tag2b', ' tag2c']},
{'id':3,
'title':'title3',
'text':'bla3 bla3 bla3',
'tags':[' tag3a', ' tag3b', ' tag3c']}]
and so on.
I am trying to go into each dictionary in the list and strip out the left whitespaces and return a list of dictionaries where the only difference are the tags have their uneccessary white space stripped.
The following code is what I am working with, but it is not right and I don’t know what I am doing to get to the result i need.
notes_cleaned = []
for objs in notes:
for items in objs:
notes_cleaned.append({'text':n['text'], 'id':n['id'], 'tags':[z.lstrip(' ') for z in n['tags']], 'title':n['title']})
Which gives me an error that i can’t use string indexes, which I understand, but I don’t know how to do it right. since I know that I have to iterate over each dictionary like:
for objs in notes:
for items in objs:
print items, objs[items]
but I am confused as to how to get to the final part of rebuilding the dictionaries while digging into the tag lists specifically.
What am I missing here (knowing that I am definitely missing something).
The following code should work, assuming only “tags” needs to be stripped: