Advertisement
here2share

# list_comprehension.py

Apr 8th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.26 KB | None | 0 0
  1. # list_comprehension.py -- basically a for loop that returns a list
  2.  
  3.  
  4. # the long code:
  5.  
  6. mylist = []
  7. for x in [1,2,3]:
  8.     mylist.append("x:" + str(x))
  9.  
  10. print mylist
  11. # ["x:1", "x:2", "x:3"]
  12.  
  13.  
  14. # the short code:
  15. print ["x:" + str(x) for x in [1,2,3]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement