Home > Net >  How to copy some elements of a list based in it's position?
How to copy some elements of a list based in it's position?

Time:07-07

I'm making a simple grocery buying program that uses 2 fixed lists and 2 that can have elements added by the user.

product_list = ['apple', 'orange', 'watermelon', 'banana', 'coconut']
price_list = [ 5.32 , 6.45 , 2.37 , 5.32, 6.45 ]

bought_product_list = [ ]
bought_price_list = [ ]


while True:
    
    product_code = input('Enter the product code: ')
    
    if product_code not in product_list:
        print('Invalid product code! Try again!')

    if product_code in product_list:
            quantity = int(input('Enter the quantity:'))
            for q in range(quantity):
                bought_product_list.append(product_code)

I can add the product_code to the bought_price_list, but I need a way to import the correct price associated with the product_code from the other lists.

Let's say the user types apple with the quantity 2 and watermelon with the quantity 3, the output should be:

bought_product_list = [ 'apple', 'apple', 'watermelon' , 'watermelon' , 'watermelon' ]
bought_price_list = [ 5.32 , 5.32 , 2.37 , 2.37 , 2.37 ]

Can someone help me with this question?

CodePudding user response:

Add this at the end of your code:

bought_price_list.append(price_list[product_list.index(product_code)])

or the better way is to make an dict from them like this:

product_list = ['apple', 'orange', 'watermelon', 'banana', 'coconut']
price_list = [ 5.32 , 6.45 , 2.37 , 5.32, 6.45 ]

product_dict = {i:j for i,j in zip(product_list, price_list)}

Then you have:

{'apple': 5.32,
 'orange': 6.45,
 'watermelon': 2.37,
 'banana': 5.32,
 'coconut': 6.45}

so change your code inside the while:

product_code = input('Enter the product code: ')

if product_code not in product_list:
    print('Invalid product code! Try again!')

if product_code in product_dict:
    quantity = int(input('Enter the quantity:'))
    for q in range(quantity):
        bought_product_list.append(product_code)
        bought_price_list.append(product_dict[product_code])
         

Also you can enhance for q in range(quantity): like this:

if product_code in product_dict:
   quantity = int(input('Enter the quantity:'))
   bought_product_list  = [product_code] * quantity
   bought_price_list  = product_dict[product_code] * quantity

CodePudding user response:

first of all you don't need second check. You already make check before it. So it would be better to skip current iteration. Remove this line

if product_code in product_list:  ## delete this line

and edit this one:

if product_code not in product_list:
    print('Invalid product code! Try again!')
    continue

Then you need to find proper price for the product. This can be done using index method on the list.

and lastly you insert the data. Finally code will look like this:

product_list = ['apple', 'orange', 'watermelon', 'banana', 'coconut']
price_list = [ 5.32 , 6.45 , 2.37 , 5.32, 6.45 ]

bought_product_list = [ ]
bought_price_list = [ ]


while True:
    
    product_code = input('Enter the product code: ')
    
    if product_code not in product_list:
        print('Invalid product code! Try again!')
        continue
    
    # get index of product code to extract product price
    product_code_idx = product_list.index(product_code)
    product_price = price_list[product_code_idx]

    quantity = int(input('Enter the quantity:'))

    bought_product_list.extend([product_code] * quantity)
    bought_price_list.extend([product_price] * quantity)

CodePudding user response:

Consider using a dictionary for this use case. A solution for your problem would be to use a single dictionary with the product as key and price as value. To strictly adhere to your output, without changing your conditions, you can do this:

product_list = {'apple': 5.32, 'orange': 6.45, 'watermelon': 2.37, 'banana': 5.32, 'coconut': 6.45}
bought_product_list = []


while True:
    
    product_code = input('Enter the product code: ')
    
    if product_code not in product_list.keys():
        print('Invalid product code! Try again!')

    if product_code in product_list.keys():
            quantity = int(input('Enter the quantity:'))
            for q in range(quantity):
                bought_product_list.append(product_code)
                bought_price_list.append(product_list[product_code])

however I would advise to use else on the second condition.

  • Related