Accessing Python 3x dictionary values for a variable dictionary name

python dictionary
python dictionary methods
python dictionary keys
python dictionary append
python dictionary of dictionaries
sort dictionary by value python
python print dictionary keys and values
python return dictionary

I have a Python 3.7.3 file, specs.py, containing multiple dictionaries with the same keys.

f150 = {
    'towing-capacity' : '3,402 to 5,897 kg',
    'horsepower' : '385 to 475 hp',
    'engine' : ' 2.7 L V6, 3.3 L V6, 3.5 L V6, 5.0 L V8'
}

f250 = {
    'towing-capacity' : '5,670 to 5,897 kg',
    'horsepower' : '290 to 450 hp',
    'engine' : '6.2 L V8, 6.7 L V8 diesel, 7.3 L V8'
}

In another file, I am importing specs.py and would like to be able to find the value associated with a given key for the variable carmodel.

hp = specs.{what should I put here so it equals cardmodel}.['horsepower']

you can use

getattr(specs, carmodel)['horsepower']

Because global variables will be attributes on a module object.

But it would make more sense maybe to nest your dict further:

cars = {
'f150': {
    'towing-capacity' : '3,402 to 5,897 kg',
    'horsepower' : '385 to 475 hp',
    'engine' : ' 2.7 L V6, 3.3 L V6, 3.5 L V6, 5.0 L V8'
},

'f250' : {
    'towing-capacity' : '5,670 to 5,897 kg',
    'horsepower' : '290 to 450 hp',
    'engine' : '6.2 L V8, 6.7 L V8 diesel, 7.3 L V8'
}}}

Then you can use like:

specs.cars[carmodel]['horsepower']

Python 3 - Dictionary, Python 3 - Dictionary - Each key is separated from its value by a colon (:), the items are To access dictionary elements, you can use the familiar square brackets along dict['Name'] # remove entry with key 'Name' dict.clear() # remove all entries in dict If passed variable is dictionary, then it would return a dictionary type. Find the Value Using Index Operator in Python. In addition to the above method, you can also get the values by keys in Dictionary. You have to use the Python index operator([]) to find the value of the given key. The index operator requires a single argument to pass. You have to just pass the known key as the argument of the index operator. See the below method to get the use of index operator to get values.

You can reference any attribute of any object (modules are objects too) by string in python using getattr

import specs
getattr(specs, 'f150')

Dictionary Data Structures in Python 3, In this tutorial, we will go over the dictionary data structure in Python. Dictionaries behave like a database in that instead of calling an integer to get a particular index In our example, that would use the variable name and be sammy.keys() . Let’s discuss various ways of accessing all the keys along with their values in Python Dictionary. Method #1 : Using in operator Most used method that can possibly get all the keys along with its value, in operator is widely used for this very purpose and highly recommended as offers a concise method to achieve this task.

You can do this like this:

import specs
specs.f150                                                                                                                                                                          

#{'towing-capacity': '3,402 to 5,897 kg',
# 'horsepower': '385 to 475 hp',
# 'engine': ' 2.7 L V6, 3.3 L V6, 3.5 L V6, 5.0 L V8'}

specs.f150['horsepower']                                                                                                                                                            
# '385 to 475 hp'

Python Tutorial: Dictionaries, Tutorial on Dictionaries in Python: Operators and Methods of the Dictionary class. Any key of the dictionary is associated (or mapped) to a value. Therefore it is not possible to access an element of the dictionary by a number, like we did with Using "talking" variable names in intermediate steps can enhance legibility. The dictionary variable in Python works like an associated array in other programming languages. The elements are indexed with the key elements. To access the elements of the dictionary variable, you have to use the index operator ([]). The index operator requires one argument as the key position of the elements.

Python Dictionaries, The main differentiating factor between a list and a dictionary would be, that instead of the index we use keys to access the elements of a dictionary (or values to� You can assign a dictionary value to a variable in Python using the access operator [].

Dictionaries and strings, A dictionary with the city name as index is more convenient, because this allows us to Python also has a special dictionary type OrderedDict where the key- value pairs Note that if two variables refer to the same dictionary and we change the while in Python version 3.x, temps.keys() only enables iterating over the keys. To loop or iterate over each element of a dictionary variable, you have to use the for loop of Python. You can get the dictionary variable keys and values in the output. Use the for loop of the Python and use only keys or values in your programming. In the dictionary variable, the keys are related to its relevant values.

Built-in Types — Python v3.0.1 documentation, All other values are considered true — so objects of many types are always true. For other containers see the built in dict and set classes, and the collections module. the numeric index of a positional argument, or the name of a keyword argument. __missing__() must be a method; it cannot be an instance variable. Loop Through a Dictionary. You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.