Given the following three list, how would you create a new list that matches the desired output printed below?

Advertisement

Given the following three list, how would you create a new list that matches the desired output printed below?

fruits = [‘Apples’, ‘Oranges’, ‘Bananas’]
quantities = [5, 3, 4]
prices = [1.50, 2.25, 0.89]

#Desired output
[(‘Apples’, 5, 1.50),
(‘Oranges’, 3, 2.25),
(‘Bananas’, 4, 0.89)]

  • output = []
    fruit_tuple_0 = (first[0], quantities[0], price[0])
    output.append(fruit_tuple)
    fruit_tuple_1 = (first[1], quantities[1], price[1])
    output.append(fruit_tuple)
    fruit_tuple_2 = (first[2], quantities[2], price[2])
    output.append(fruit_tuple)
    return output
  • i = 0
    output = []
    for fruit in fruits:
    temp_qty = quantities[i]
    temp_price = prices[i]
    output.append((fruit, temp_qty, temp_price))
    i += 1
    return output
  • groceries = zip(fruits, quantities, prices)
    return groceries
    >>> [
    (‘Apples’, 5, 1.50),
    (‘Oranges’, 3, 2.25),
    (‘Bananas’, 4, 0.89)
    ]
  • i = 0
    output = []
    for fruit in fruits:
    for qty in quantities:
    for price in prices:
    output.append((fruit, qty, price))
    i += 1
    return output
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Leave a Comment


Share via
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Send this to a friend