example = ['1 Hey this is the first (1) level.\n', '2 This is what we call a second level.\n','','3 This is the third (3) level, deal with it.']
example = [i.rstrip() for i in example]
dictHeaders = [['1 ','One'],
['2 ','Two'],
['3 ','Three'],
['4 ','Four'],
['5 ','Five'],
['6 ','Six']]
example = [eachLine.replace(old,new,1) if eachLine.startswith(old) for eachLine in article for old, newFront in dictHeaders]
I need it to return...
example = ['One Hey this is the first (1) level.', 'Two This is what we call a second level.','','Three This is the third (3) level, deal with it.']
I have created dictHeaders
as a list of lists for a reason to add move values into each key for each instance. For example, if eachLine.startswith(old)
then append One
to the beginning and possible another string to the end of the line. If I can do the above with a dict, I'd rather go that route.. I'm new to Python.
I thought this is the route to go instead of...
def changeCode(example,dictHeaders):
for old, newFront in dictHeaders:
for eachLine in example:
if eachLine.startswith(old):
return eachLine.replace(old,newFront,1)
Every time I run the above it only returns the top line, but I need it to return the entire list example
modified..