-6

I have three lists. I'd like to look up the content of two of them based on picking an item (GTIN8 code) in the first list:

code = [12345670,12234561,12334568,12344567,12345564,12345663]
name = ['Milk (1 Pints)','Dognuts 6PK','Cookies 2PK','Apples(1KG)','Strawberries(500g)','Bananas(500g)']
price = [0.25,0.50,0.50,1.00,1.25,0.65]

Code= int(input('enter code: '))

How do I do that?

roadrunner66
  • 7,482
  • 4
  • 30
  • 37
  • what is the intended solution/question ? the heading is not self explanatory – minocha May 22 '16 at 16:39
  • 3
    Please clarify what you exactly want to do. Specify the expected output for some input. See http://stackoverflow.com/help/how-to-ask – Keiwan May 22 '16 at 16:39
  • What exactly are you asking for, what is your specific problem? – tschale May 22 '16 at 16:40
  • What's the problem, exactly, and what are you trying to achieve? I can't understand what you mean by 'index of input'. – Pavel Gurkov May 22 '16 at 16:41
  • 1
    so like user will input a GTIN8 code if it is in the list(code) I want assign a variable to index of the input – Atharva Tidke May 22 '16 at 16:42
  • 1
    I edited the question heading which hopefully aligns with the OP's intent and clarifies what the commenters asked for. – roadrunner66 May 22 '16 at 16:56
  • 2
    Possible duplicate of [Finding the index of an item given a list containing it in Python](http://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – tschale May 22 '16 at 17:19

2 Answers2

2

While it works to use the list index, you probably should use a dictionary with the code as the key, and a tuple of the name and the price as the value:

items = {
    12345670: ('Milk (1 Pints)', 0.25),
    12234561: ('Dognuts 6PK', 0.50),
    # [...]
}

I would also add a check whether the user specified code acutally exists:

code = int(raw_input("Please enter the GTIN8 code: "))
if code in items:
    print items[code][0], items[code][1]
else:
    print "This code does not exist!"

Or, more pythonic with try-except:

code = int(raw_input("Please enter the GTIN8 code: "))
try:
    print items[code][0], items[code][1]
except KeyError:
    print "This code does not exist!"
tschale
  • 972
  • 1
  • 18
  • 34
  • 2
    Using a dictionary and then `code in items.keys()` in Python 2.x is a bad idea. This builds a list of all keys and then does a linear search which ignores the reason to use a dictionary: fast lookup. `if code in items:` uses this trait of `dict`. `raw_input()` can take a prompt as argument. – BlackJack May 22 '16 at 17:30
  • Thank you, I edited the answer accordingly – tschale May 22 '16 at 17:36
1

You are making three lists where a different data structure, say a dictionary might be better suited. If you change one of the lists and forget to change the others, you'll have issues.

Apart from that you trying to look up an item in a 2nd list based on the location of an item in a first list.

Based on that answer you could do something like this.

code = [12345670,12234561,12334568,12344567,12345564,12345663]
name = ['Milk (1 Pints)','Dognuts 6PK','Cookies 2PK','Apples(1KG)','Strawberries(500g)','Bananas(500g)']
price = [0.25,0.50,0.50,1.00,1.25,0.65]

a=int(raw_input())
i=code.index(a)
print name[i],price[i]

output:

12345670
Milk (1 Pints) 0.25

where I used Python 2.7 ( raw_input instead of input, and leaving out parentheses in the print statement).

Community
  • 1
  • 1
roadrunner66
  • 7,482
  • 4
  • 30
  • 37
  • upvote takes at least 10 reputation but @AtharvaTidke if you accept the answer (used this method / it answered your question) then please do our site a favour and [label it appropriately](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Tadhg McDonald-Jensen May 22 '16 at 19:50