464,606 Members | 1,140 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,606 IT Pros & Developers. It's quick & easy.

# Get item of a list by index

 P: 6 Hey, I made a programm that looks like this, but I have a problem. Expand|Select|Wrap|Line Numbers command = raw_input(">") fullcommand = command.split(" ") #example user input: >add 10 15 if fullcommand.index("add") == 0: #how I want the rest to work: * * *a= fullcommand.getItem(1) * * *b= fullcommand.getItem(2) * * *c= a + b * * *print ("Result: *" + c)   So what I need is a function that gets the item of a list which has the index 1 and the item with the index 2. How does that work? Peter Feb 4 '12 #1

#### ✓ answered by Smygis

Expand|Select|Wrap|Line Numbers
1. command = raw_input(">")
2. fc = command.split()
3. if fc[0] == "add":
4.     a = fc[1]
5.     b = fc[2]
6.     c = a + b
7.     print "Result: " + c
Does exactly what you described that it should do. (I'm using python 2.7, you'll have to add parentheses at the print function if you are using python 3.x)

 100+ P: 126 Expand|Select|Wrap|Line Numbers command = raw_input(">") fc = command.split() if fc[0] == "add":     a = fc[1]     b = fc[2]     c = a + b     print "Result: " + c Does exactly what you described that it should do. (I'm using python 2.7, you'll have to add parentheses at the print function if you are using python 3.x) Feb 4 '12 #2