Reputation: 29
I am trying to get a variable to get a string out of a list (which is a variable name), and then I want to call that variable.
There is a list which stores a lot of variable names, and some other variables.
fruits = ["apple","orange","banana","blueberry"]
apple = ["A red fruit","sort of ball"]
orange = ["An orange fruit","a ball"]
banana = ["A yellow fruit","a fruit"]
blueberry = ["A blue fruit","a berry"]
number = 3
I want to get the fruit from the list that I have the 'number' for:
print(fruits[number])
That will output: banana
The output is the name of a pre-existing list variable, so how do I call an item from that list?
I tried doing this:
print((fruits[number])[2])
Which I thought may come out like this:
print(banana[2])
And output: a fruit
Thank you in advance for your help.
Upvotes: 1
Views: 316
Reputation: 26
To use a variable like you want isn't a good idea. In this case, the best way to handle what you want to do is using a "dict". This is a native "key: value" python's data structure.
You can define the fruits and their descriptions like this:
fruits = {'banana': ['A yellow fruit', 'a fruit'],
'orange': ['An orange fruit', 'a ball'],
...
...}
Then if you want to print the description of some fruit you should use its key:
fruit_to_print = 'banana'
print fruits[fruit_to_print]
When run this will output:
['A yellow fruit', 'a fruit']
If you want to get, for example, the first item of the description:
print fruits[fruit_to_print][0]
which will output:
A yellow fruit
Dictionaries are not intended to be an ordered structure, so you shouldn't call its values using an index, but if you're REALLY sure of what you're doing you can do this:
fruits = {'banana': ['A yellow fruit', 'a fruit'],
'orange': ['An orange fruit', 'a ball']}
number = 1
desc_index = 0
# fruits.keys() returns an array of the key names.
description = fruits[fruits.keys()[number]][desc_index]
print description
>>> A yellow fruit
Bear in mind that adding or removing an element can potentially change the index of every other element.
Another way is to create a class Fruit and have an array of Fruit(s):
class Fruit:
def __init__(self, name, description):
self.name = name
self.description = description
fruit1 = Fruit('banana', ['A yellow fruit', 'a fruit'])
fruit2 = Fruit('orange', ['An orange fruit', 'a ball'])
fruits = []
fruits.append(fruit1)
fruits.append(fruit2)
fruit = 1
description = 0
print fruits[fruit].description[description]
An orange fruit
Upvotes: 1
Reputation: 3163
This is not possible. The closest you can get is by using dictionaries, which are basically maps from keys to values. In your case, a dictionary would look like this:
fruits = {
"apple": ["A red fruit","sort of ball"],
"orange" : ["An orange fruit","a ball"],
"banana": ["A yellow fruit","a fruit"],
"blueberry": ["A blue fruit","a berry"]
}
Now you can do something like print(fruits['banana'][1])
which will print a fruit
since fruits
is a dictionary, 'banana'
is a key in this dictionary, and fruits[banana]
is equal to ["A yellow fruit","a fruit"]
. When you access element on index 1
of fruits['banana']
, it returns second element of that list, since lists are 0-indexed in Python, and that is a string a fruit
.
Upvotes: 3
Reputation: 345
What you want is called an associative array, in python these are called dictionaries.
fruitsDict = {}
fruitsDict["apple"] = ["A red fruit","sort of ball"]
fruitsDict["orange"] = ["An orange fruit","a ball"]
fruitsDict["banana"] = ["A yellow fruit","a fruit"]
fruitsDict["blueberry"] = ["A blue fruit","a berry"]
If you want to get the keys of the dictionary as strings you can use
for key, value in fruitsDict.items():
print(key,value[1])
Output:
Apple sort of ball
Orange a ball
Banana a fruit
Blueberry a berry
Click here for a working example in tutorials point
Upvotes: 3