bitfucker , (edited )

You can. Whatever the method returns will be the element of that list. So if for example I do this:

def mul(x):
  return x*2

list = [mul(value) for value in range(1,20)]

It will have the same effect. But this:

def mul(x):
  return

list = [mul(value) for value in range(1,20)]

Will just makes the list element all None

Edit to add more:
List comprehension works not from the range function. Rather, the range function is returning a list. Hence the name, "list comprehension". You can use any old list for it.

What it did under the hood is that it iterates each element on the list that you specify (the in ...), and applies those to the function that you specify in the very first place. If you are familiar with the concept of Array.map in other languages, this is that. There is also a technical explanation for it if it helps, but it requires more time to explain. Just let me know if you would like to know it.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • kbinchat
  • All magazines