daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Original list of numbers.
  2. numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  3.  
  4. # For loop to double the numbers in a list.
  5. doubled_numbers = []
  6. for number in numbers:
  7.   doubled_numbers.append(number * 2)
  8.  
  9. # Rewrite using list comprehension.
  10. # Syntax:
  11. #   list_object = [expression_on_item for_item_in_iterable]
  12. doubled_numbers = [number * 2 for number in numbers]
  13.  
  14. print(doubled_numbers) # Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top