I am trying to either
create dictionaries names based on loop index e.g. mydict_1, mydict_2 etc.
or
append dictionaries in one dictionary
Through a loop I am getting sets of data and I want to be able to access them all at once or one by one.
for components in fiSentence.findall("components"):
operation = components.find('operation').text
target = components.find('target').text
targetState = components.find('targetState').text
...
all this going in a dictionary:
tempDict = {"operation":operation, "target":target, "targetState":targetState, ...}
and then outside of the loop I tried to store all of them in another dictionary but I only managed to do so with a list:
data.append(tempDict)
What I want is either to store them in different dictionaries as:
procedural_Step_1 = {"operation":operation, "target":target, "targetState":targetState}
procedural_Step_2 = {"operation":operation, "target":target, "targetState":targetState}
...
or
store them all in one dictionary of dictionaries:
data = {"procedural_Step_1":{"operation":operation, "target":target, "targetState":targetState}, {"procedural_Step_2":{"operation":operation, "target":target, "targetState":targetState},...}
You can declare dict data before the loop and in the end of loop:
data['procedural_step_'+str(index)] = temp_dict
Index you can get with enumerate
Related
I have the following list of values: Numbers = [1,2,3,4].
Is it possible to create a dictionary with the same name as the values contained in the list?
Example: dictionary_1 = {}
dictionary_2 = {}
....
dictionary_Number.. {}
I would like to create these dictionaries automatically, without creating them manually, reading the numbers contained in the list
You may use the keyword exec in python. Here is an example of your solution,
List = [1, 2,3]
for ele in List:
dic = f"Dictionary_{ele}"
exec(dic+" = {}")
print(Dictionary_1, Dictionary_2, Dictionary_3, sep='\n')
you may use it according to you, but the disadvantage for it is that you will need to use exec every time you need to use it or you must know what would be the name outcome of the first use of exec ...
I hope I helped...
Use the inbuild functions and remember that a dictionary needs a tuble (key & value):
Python Dictionaries
Python Dictionary fromkeys() Method
Example-Code:
Numbers = [1,2,3,4]
Numbers_dict = dict.fromkeys(Numbers,"dict_value")
print(Numbers_dict)
This will output:
{'1': 'dict_value', '2': 'dict_value', '3': 'dict_value', '4': 'dict_value'}
If you want to get a single dictonaries for each list-value, you first have to create for each list-value an empty variable.
After this you have to fill this empty vairables within a loop.
If I have a list of dictionaries in a python script, that I intend to later on dump in a JSON file as an array of objects, how can I index the keys of a specific dictionary within the list?
Example :
dict_list = [{"first_dict": "some_value"}, {"second_dict":"some_value"}, {"third_dict": "[element1,element2,element3]"}]
My intuitive solution was dict_list[-1][0] (to access the first key of the last dictionary in the list for example). This however gave me the following error:
IndexError: list index out of range
the key inputted into the dictionary will pick the some value in the format dict = {0:some_value}
to find a specific value:
list_dictionary = [{"dict1":'value1'},{"dict2","value2"}]
value1 = list_dictionary[0]["dict1"]
the 'key' is what you have to use to find a value from a dictionary
Example:
dictionary = {0:value}
dictionary[0]
in this case it will work
but to pick the elements we will do
values = []
for dictionary in dict_list:
for element in dictionary:
values.append(dictionary[element])
Output:
['some_value', 'some_value', ['element1', 'element2', 'element3']]
dict_list = [{"first_dict": "some_value"}, {"second_dict":"some_value"}, {"third_dict": ['element1','element2','element3']}]
If your dict look like this you can do as well
dict_list[-1]["third_dict"]
You can't access 'the first key' with a int since you have a dict
You can get the first key with .keys() and then
dict_list[-1].keys()[0]
By using dict_list[-1][0], you are trying to access a list with a list, which you do not have. You have a list with a dict key within a list.
Taking your example dict_list[-1][0]:
When you mention dict_list you are already "in the list".
The first index [-1] is referring to the last item of the list.
The second index would only be "usable" if the item mentioned in the previous index were a list. Hence the error.
Using:
dict_list=[{"first_dict": "some_value"}, {"second_dict":"some_value"},{"third_dict": [0,1,2]}]
to access the value of third_dict you need:
for value in list(dict_list[-1].values())[0]:
print(value)
Output:
0
1
2
If you know the order of dictionary keys and you are using one of the latest python versions (key stays in same order), so:
dict_list = [
{"first_dict": "some_value"}
, {"second_dict":"some_value"}
, {"third_dict": ["element1", "element2", "element3"]}
]
first_key = next(iter(dict_list[-1].keys()))
### OR: value
first_value = next(iter(dict_list[-1].values()))
### OR: both key and value
first_key, first_value = next(iter(dict_list[-1].items()))
print(first_key)
print(first_key, first_value)
print(first_value)
If you have the following list of dictionaries:
dict_list = [{"key1":"val1", "key2":"val2"}, {"key10":"val10"}]
Then to access the last dictionary you'd indeed use dict_list[-1] but this returns a dictionary with is indexed using its keys and not numbers: dict_list[0]["key1"]
To only use numbers, you'd need to get a list of the keys first: list(dict_list[-1]). The first element of this list list(dict_list[-1])[0] would then be the first key "key10"
You can then use indices to access the first key of the last dictionary:
dict_index = -1
key_index = 0
d = dict_list[dict_index]
keys = list(d)
val = d[keys[key_index]]
However you'd be using the dictionary as a list, so maybe a list of lists would be better suited than a list of dictionaries.
Pardon the noob question, I am new to Python.
I am working with a list of dictionaries:
dataset = [
{'id':1,'dateCollected':'2021-03-02','orders':7},
{'id':2,'dateCollected':'2021-03-03','orders':8},
]
.... this goes on for 50 records ....
I would like to a make a for loop that iterates through every dictionary in the list and adds certain key-value pairs to a new dictionary.
For Example:
match_history_data = {}
for i in dataset:
match_history_data['DateCollected'] = i['dateCollected']
match_history_data['Orders'] = i['orders']
print(match_history_data)
results in:
{'DateCollected': '2021-03-03', 'Orders': 8}
I would like the result to be:
{'DateCollected':'2021-03-02','Orders':7},
{'DateCollected':'2021-03-03','Orders':8}
This works how I want it to, but only for the first iteration in the for loop. How do I get around this so that it goes thru all the records in 'dataset', and creates the new list or dictionary?
You're creating a new dictionary, match_history_data and then just setting the same entries 'DateCollected' and 'Orders' over and over again. Each iteration of your for loop is just overwriting the same entries.
What you want is a list of new dictionaries:
match_history_data = []
for item in dataset:
match_history_data.append(
{"DateCollected" : item["dateCollected"],
"Orders" : item["orders"]})
You can achieve this in a neater fashion using a list comprehension:
match_history_data = [{"DateCollected" : item["dateCollected"], "Orders" : item["orders"]} for item in dataset]
How about this
match_history_data = [{"DateCollected":x["dateCollected"], "Orders":x["orders"]} for x in dataset]
What you have right now overwrites a single dictionary as your loop through the original list of dictionaries, since keys are unique in dictionaries. What you need to is another list to which you can append the new dictionaries during each iteration to a separate list.
For example
new_list = []
for i in dataset:
match_history_data = {}
match_history_data['DateCollected'] = i['dateCollected']
match_history_data['Orders'] = i['orders']
new_list.append(match_history_data)
If you don't want to preserve the original list, you can simply drop the id key in each dictionary.
i'm using an api call in python 3.7 which returns json data.
result = (someapicall)
the data returned appears to be in the form of two nested dictionaries within a list, i.e.
[{name:foo, firmware:boo}{name:foo, firmware:bar}]
i would like to retrieve the value of the key "name" from the first dictionary and also the value of key "firmware" from both dictionaries and store in a new dictionary in the following format.
{foo:(boo,bar)}
so far i've managed to retrieve the value of both the first "name" and the first "firmware" and store in a dictionary using the following.
dict1={}
for i in result:
dict1[(i["networkId"])] = (i['firmware'])
i've tried.
d7[(a["networkId"])] = (a['firmware'],(a['firmware']))
but as expected the above just seems to return the same firmware twice.
can anyone help achive the desired result above
you can use defaultdict to accumulate values in a list, like this:
from collections import defaultdict
result = [{'name':'foo', 'firmware':'boo'},{'name':'foo', 'firmware':'bar'}]
# create a dict with a default of empty list for non existing keys
dict1=defaultdict(list)
# iterate and add firmwares of same name to list
for i in result:
dict1[i['name']].append(i['firmware'])
# reformat to regular dict with tuples
final = {k:tuple(v) for k,v in dict1.items()}
print(final)
Output:
{'foo': ('boo', 'bar')}
I'm trying to create many dictionaries in a for loop in Python 2.7. I have a list as follows:
sections = ['main', 'errdict', 'excdict']
I want to access these variables, and create new dictionaries with the variable names. I could only access the list sections and store an empty dictionary in the list but not in the respective variables.
for i in enumerate(sections):
sections[i] = dict()
The point of this question is. I'm going to obtain the list sections from a .ini file, and that variable will vary. And I can create an array of dictionaries, but that doesn't work well will the further function requirements. Hence, my doubt.
Robin Spiess answered your question beautifully.
I just want to add the one-liner way:
section_dict = {sec : {} for sec in sections}
For maintaining the order of insertion, you'll need an OrderedDict:
from collections import OrderedDict
section_dict = OrderedDict((sec, {}) for sec in sections)
To clear dictionaries
If the variables in your list are already dictionaries use:
for var in sections:
var.clear()
Note that here var = {} does not work, see Difference between dict.clear() and assigning {} in Python.
To create new dictionaries
As long as you only have a handful of dicts, the best way is probably the easiest one:
main = {} #same meaning as main = dict() but slightly faster
errdict = {}
excdict = {}
sections = [main,errdict,excdict]
The variables need to be declared first before you can put them in a list.
For more dicts I support #dslack's answer in the comments (all credit to him):
sections = [dict() for _ in range(numberOfDictsYouWant)]
If you want to be able to access the dictionaries by name, the easiest way is to make a dictionary of dictionaries:
sectionsdict = {}
for var in sections:
sectionsdict[var] = {}
You might also be interested in: Using a string variable as a variable name