Advertisement
elena1234

comprehensions in Python

Mar 1st, 2023 (edited)
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | Source Code | 0 0
  1. # This is a dictionary comprehension
  2. # We'll see an example after this assignment!
  3.  
  4. euro_data = {
  5.     item_id: [name, price, category, sizes]
  6.     for item_id, name, price, category, sizes in zip(
  7.         item_ids_list, item_names_list, euro_prices_list, item_category_list, sizes_list
  8.     )
  9. }
  10.  
  11. euro_data
  12.  
  13. ''' 2  Calculate the Total Cost of Items
  14.  
  15. Calculate the amount it would cost to purchase all the items in euro_data.
  16.  
  17. Use a list comprehension to collect them into a list. '''
  18.  
  19.  
  20. sum([euro_data[key][1] for key, value in euro_data.items()])
  21.  
  22. # new_list = [expression for member in other_iterable (if condition)]
  23. # new_dict = {key: value for key, value in other_iterable (if condition)}
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement