Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 - Multiplying numbers in a list by two

The purpose of the code I'm asked to complete is to receive the input of the given inventories, return them as given in a list in one line. Then on a second line, duplicate the list but this time double the numbers.

The given inputs are

Choc 5; Vani 10; Stra 7; Choc 3; Stra 4

The desired output is:

[['Choc', 5], ['Vani', 10], ['Stra', 7], ['Choc', 3], ['Stra', 4]]
[['Choc', 10], ['Vani', 20], ['Stra', 14], ['Choc', 6], ['Stra, 8]]

I've managed to successfully get the desired output for the first line, but am struggling with how to successfully compete the second.

This is the code:

def process_input(lst):
    result = []
    for string in lines:
        res = string.split()
        result.append([res[0], int(res[1])])
    return result

def duplicate_inventory(invent):
    # your code here
    return = []
    return result

# DON’T modify the code below
string = input()
lines = []
while string != "END":
    lines.append(string)
    string = input()
inventory1 = process_input(lines)
inventory2 = duplicate_inventory(inventory1)
print(inventory1)
print(inventory2)
like image 749
smitty23 Avatar asked Jan 02 '23 13:01

smitty23


1 Answers

Since you already have the first line done, you can use a simple list comprehension to get the second line:

x = [[i, j*2] for i,j in x]
print(x)

Output:

[['Choc', 10], ['Vani', 20], ['Stra', 14], ['Choc', 6], ['Stra', 8]]
like image 183
user3483203 Avatar answered Jan 05 '23 19:01

user3483203