简体   繁体   中英

How to Remove Something in a List in a Dictionary in Python

There isn't much to this question but I can't find it anywhere else, so I'm asking it.

How do you search for something that is in a List inside a Dictionary in Python.

Here's an example:

mydict = {"aaron":["1","4","10\n","3","6"] , "paul":["7","4\n","6","15"]}
# Additional code here
print(mydict)

>>> {"aaron":["1","4","10","3","6"] , "paul":["7","4","6","15"]} #Notice the '\n' has disappeared...

I need to know what code would enable me to search through each part of the value's list on each key in the dictionary, removing any "\\n" inside any part of the dictionary.

Any help will be much appreciated, thanks :) - Aaron

Instead of searching for a '\\n' in your list to remove it, just apply strip to all your lists.

In [1604]: mydict = {"aaron":["1","4","10\n","3","6"] , "paul":["7","4\n","6","15"]}

In [1605]: for i in mydict.keys():
   ......:     mydict[i] = map(str.strip, mydict[i])
   ......:     

In [1606]: mydict
Out[1606]: {'aaron': ['1', '4', '10', '3', '6'], 'paul': ['7', '4', '6', '15']}

You could loop through the dictionary, get the list, which is value, then loop through the values in the list and reset them without the newline character. Then reset the list for the key

mydict = {"aaron":["1","4","10\n","3","6"] , "paul":["7","4\n","6","15"]}
print mydict
for key, value in mydict.iteritems():
    for ind, item in enumerate(value):
         value[ind] = item.strip()
print mydict
{'aaron': ['1', '4', '10\n', '3', '6'], 'paul': ['7', '4\n', '6', '15']}
{'aaron': ['1', '4', '10', '3', '6'], 'paul': ['7', '4', '6', '15']}
>>> mydict = {"aaron":["1","4","10\n","3","6"] , "paul":["7","4\n","6","15"]}
>>> mydict = {k:[element.strip() for element in mydict.get(k)] for k in mydict}
>>> mydict
{'paul': ['7', '4', '6', '15'], 'aaron': ['1', '4', '10', '3', '6']}

If you want to iterate through the dictionary, you can use:

for key,value in mydict.items():
    print (key,value)

In order to remove the '\\n' from the strings, you can iterate and check if the string contains '\\n', then remove this substring out:

for (key, value) in mydict.items():
    for item in value:
        item = item.replace('\n','')
In [1]: mydict = {"aaron":["1","4","10\n","3","6"] , "paul":["7","4\n","6","15"]}

In [2]: for k,L in mydict.items():
   ...:     for i,elem in enumerate(L):
   ...:         L[i] = elem.strip()
   ...:         

In [3]: mydict
Out[3]: {'aaron': ['1', '4', '10', '3', '6'], 'paul': ['7', '4', '6', '15']}

OP's code suggests Python 3 and he did like another comprehension, so:

mydict = {k: list(map(str.strip, v)) for k, v in mydict.items()}

or

mydict = {k: list(map(str.strip, mydict[k])) for k in mydict}
In [11]: mydict = {"aaron":["1","4","10\n","3","6"] , "paul":["7","4\n","6","15"]}

In [12]: dict(map(lambda (k,v): (k, [item.replace("\n","") for item in v]), mydict.iteritems()))
Out[12]: {'aaron': ['1', '4', '10', '3', '6'], 'paul': ['7', '4', '6', '15']}

if there is only \\n you can do it through this piece of code:

for i in mydict:
    mydict[i]=list(map(lambda s:str(int(s)),mydict[i]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM