Advertisement
Guest User

Understanding of List comprehension

a guest
Jul 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. color = ['ref','green','white','black','pink','yellow']   ## is a python list object
  2. coloor = [x for i,x in enumerate(color) if i not in (0,4,5)]
  3. ## here using different variable coloor because otherwise the original list color will be overwrite.
  4. """
  5. ## 2nd line is using Python List Comprehensions technique which is the short form of the following code
  6. coloor = []
  7. for i,x in enumerate(color):
  8. ...     if i not in (0,4,5):  ## this condition is checking i is not in position 0, 4, 5
  9. ...         coloor.append(color[i])
  10.  
  11. """
  12. ## Note here enumerate is a python build-In function which return 2 value (counter, value)
  13.  
  14. print(coloor) ## print the new list object coloor = ['green','white','black']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement