Reputation: 57
I'm trying to make my code more 'Pythonic'.
At the moment I am calling a function 16 times, incrementing by 12 each time. I then add these variables to a dictionary and assign a key with the same name as the variable (value):
c1 = getIndividualCounts(42)
c2 = getIndividualCounts(54)
c3 = getIndividualCounts(66)
c4 = getIndividualCounts(78)
c5 = getIndividualCounts(90)
c6 = getIndividualCounts(102)
c7 = getIndividualCounts(114)
c8 = getIndividualCounts(126)
c9 = getIndividualCounts(138)
c10 = getIndividualCounts(150)
c11 = getIndividualCounts(162)
c12 = getIndividualCounts(174)
c13 = getIndividualCounts(186)
c14 = getIndividualCounts(198)
c15 = getIndividualCounts(210)
c16 = getIndividualCounts(222)
main_data = {'c1':c1, 'c2':c2, 'c3':c3, 'c4':c4, 'c5':c5, 'c6':c6, 'c7':c7, 'c8':c8, 'c9':c9, 'c10':c10, 'c11':c11, 'c12':c12, 'c013':c13, 'c14':c14, 'c15':c15, 'c16':c16}
This currently works fine, but is quite chunky. What I would like to do is the loop through the function 16 times, increasing the start index by 12 each time and auto-generating the dictionary and keys + values.
This is what I have so far:
index = 1
for i in range(16):
index += 12
getIndividualCounts(42) + index
return ({'c' + str(i) + ':'+ c + i} )
Needless to say it doesn't work. I've tried multiple iterations, but can't find any approach that works. As a relatively new Python programmer, I would also appreciate an explanation to a possible solution, so I can learn going forward.
Upvotes: 0
Views: 97
Reputation: 803
This is a possible solution
main_data = dict()
inital_value = 42
for i in range(16):
index = 'c{}'.format(i+1)
value = inital_value + i*12
main_data[index] = getIndividualCounts(value)
print(index, value)
Upvotes: 1
Reputation: 149
main_data = {}
for counter, val in enumerate(range(42, 222+1, 12)):
main_data[f'c{counter+1}'] = getIndividualCounts(val)
This would achive what you are looking for. f'' refers to format string. So you can insert varibles inside string by putting them in {}
Upvotes: 0
Reputation: 195428
The code with explanations:
# your function you want to call:
def getIndividualCounts(x):
return x
# the dictionary where you want to store data
main_data = {}
# the loop, i goes from 0, 1, ... to 15
for i in range(16):
key = 'c{}'.format(i+1) # create key (c1, c2, c3, ...)
value = getIndividualCounts(42 + i*12) # create value by calling the function (42, 54, ...)
# store the key, value inside the dictionary
main_data[key] = value
# print the data:
print(main_data)
Prints:
{'c1': 42, 'c2': 54, 'c3': 66, 'c4': 78, 'c5': 90, 'c6': 102, 'c7': 114, 'c8': 126, 'c9': 138, 'c10': 150, 'c11': 162, 'c12': 174, 'c13': 186, 'c14': 198, 'c15': 210, 'c16': 222}
Upvotes: 0
Reputation: 24232
Don't use these intermediate variables, just build the dict directly:
main_data = {}
for index in range(1, 17):
main_data['c' + str(index)] = getIndividualCounts(30 + 12*index)
or with a dict comprehension:
main_data = {'c' + str(index): getIndividualCounts(30 + 12*index) for index in range(1, 17)}
Note that, more generally, if you feel the need to have some related variables like you did in the first part of your code, you should use a list or a dict to put them in a structure, not have independant variables just related by their name.
Upvotes: 0