[Tutor] Iterate over the list

jark AJ jarkmx at gmail.com
Tue Apr 28 13:20:43 EDT 2020


excellent, thank you.
I will try this

On Tue, Apr 28, 2020 at 12:12 PM Mats Wichmann <mats at wichmann.us> wrote:

> 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))
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list