I have a python program which prints out some lists which are created by other python programs in the format of:
[0, [], 0, 1, [], 1]
And I would like to change the 0’s and 1’s to letters, i.e. in the above input, I want to have
[x, [], x, y, [], y] as output
So far my code to do this looks like:
for x in search.Search(s, s.run()):
if x == 0:
return x
elif x == 1
return y
I know I’m missing something but I’m not too sure what 🙁
Use a list comprehension:
By using
returnyou exit the loop early.The
if v else vhelps avoid attempting to use the empty lists as keys (which would raise a TypeError; lists are not hashable and thus not allowed as keys). The alternative strategy would be:The
substmap is a little easier to extend; but using a function to map the value (as used in Jacob’s answer) could be more readable to you.