I just had my lab on List Comprehension and the TA helped me understand what I didnt even know was able to be understood. Namely I learned the now obvious notion that for loops dont just have to run through numbers, they can, in fact, run through many object types (but in this case I am concerned with lists). I learned that when you write:
for i in listExample
'i' doesnt represent the index, but rather the objects in listExample. I also made the descovery that you can have multiple 'i's. For example using tuple unpacking (assuming listExample contains tuples).
for i,n in listExample
My coding style, as a result, has shifted slightly and I can condense a method into fewer lines. For example, the code I put in last week's post, which I wanted to shorten, I have now shortened to one line
def list_count(o):
return (1 + sum(list_count(i) for i in o) if isinstance(o, list) else 0)
huzza!
No comments:
Post a Comment