Can you put commands in a list?

I’m trying to place commands in lists, but it doesn’t seem to work. I want to use the .append() for the project I’m working on, so I really want it to be a list. Note that this is in Python 2.

1 Like

We wouldn’t put commands as such, but we could put their name in the list.

>>> def add(a, b):
    return a + b

>>> def subtract(a, b):
    return a - b

>>> def multiply(a, b):
    return a * b

>>> def divide(a, b):
    return a / b

>>> a_op_b = [add, subtract, multiply, divide]
>>> a, b, op = tuple(input('a b ASMD ').split())
a b ASMD 6 7 M
>>> a_op_b['asmd'.index(op.lower())](int(a), int(b))
42
>>> 

Ok, well I figured out a way around it by placing the command name in a list, checking the command names and if the names are correct running the code. Now, I’m asking what is the name portion of a dictionary?

I’ll point to the part I’m trying to ask for.

cool_list = {banana : fruit} < not the key
^
this part of it

well, spaces don’t like to work.

cool_list = {banana : fruit}

I’m asking for the bolded section. I’m trying to use an if statement that checks if that is correct, but I’m not sure how to ask for it.

The way it is written would assume that both banana and fruit are defined. If they are to be defined in this statement, they will both need to be strings.

cool_list = { 'banana': 'fruit' }

The above statement creates a dictionary named ‘cool_list’ with a ‘banana’ key associated with the string value. ‘fruit’.

print (cool_list['banana'])    # fruit

Yes, I want to be strings. But how would I check if the bolded section from before? My if statement is asking if the bolded section is correct. So, it would check if that was the correct keyword. If the bolded section was assigned to a variable, and it was checking for “print”, it would look like this.

if bold_section == “print”

Is that to mean, “does the key exist?”?


Extra Study

>>> cool_list = { 'banana': 'fruit' }
>>> try:
	print (cool_list['apple'])
except KeyError:
	print ('key {} does not exist'.format('apple'))

	
key apple does not exist
>>> try:
	print (cool_list['banana'])
except KeyError:
	print ('key {} does not exist'.format('banana'))

	
fruit
>>>
>>> if 'banana' in cool_list.keys():
	print (cool_list['banana'])

	
fruit
>>> if 'apple' in cool_list.keys():
	print (cool_list['apple'])
else:
	print ('key {} does not exist'.format('apple'))

	
key apple does not exist
>>> 

Ah, looks like this works. Thank you! Did not know the .keys() function existed

1 Like

Make a point of looking up the docs every time you learn something new. Sometimes that results in you augmenting any lesson with greater insight, and things to explore.

Well, tried looking around, doesn’t seem to be any answers that I’ve found. Looks like the .keys() won’t quite work, because I’m using the same key multiple times in the dictionary. I want it to be like this:

list = {“key1” : “tree”, “key2” : “animal”, “key1”, “banana”}

print list[2]

but print the key instead. It somehow worked here, https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/your-own-store?action=resume_content_item, but I don’t know how it found the name of the key.

This is not a list. It is a dictionary and keys cannot be duplicated, only their values replaced.

Ah, I see. Well, do you know a way or workaround to making duplicate keys in a dictionary?

No. Keys are a SET, and sets have no duplicates. Nothing stops us from it having multiple values in a list.

key: []

Ok. Could one list be the commands that the program wants the execute, then order the commands from a separate list?

We’re back to where we started. A list of names of defined functions.

Ok. Thank you for your time!

>>> a_op_b = [
    lambda a, b: a + b,
    lambda a, b: a - b,
    lambda a, b: a * b,
    lambda a, b: a / b
]
>>> a_op_b['asmd'.index(op.lower())](int(a), int(b))
42
>>> 

That’s kind of what we’re doing. The operations are in one list, the commands in another.