[Tutor] Iterate over the list

Mats Wichmann mats at wichmann.us
Tue Apr 28 12:11:42 EDT 2020


On 4/28/20 9:59 AM, jark AJ wrote:
> Hi All,
> 
> I want to iterate over the list and save the values
> Is there a better approach than the one below?
> Each of the names is unique and do not follow a pattern
> 
> names = ["name1","name2","name3","name4"]
> 
> for s in names:
>     resp = requests.get(
>     "https://{0}/APIendpoint".format(s)
> )

Your iterating is just fine.

But you are not saving the result values.  One approach (without
optimizing) might be this, which will leave you with a dictionary of
name/value pairs.



names = ["name1","name2","name3","name4"]
values = {}

for s in names:
    values[s] = requests.get("https://{0}/APIendpoint".format(s))


More information about the Tutor mailing list