Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops in List
#12
Hm!
@scaperen see my post from yesterday. When I wrote this I didn't try it with @SnadraDan's loop. Noone iterate over a list like this: for i in range(len(listA)):. So I've used for i in listA: even without thinking. Which is not working just separating the values as I've proposed previously. But it works if I use range(len(list)).

Well, when I was in the beginning with Python I used to do it in the same way. Looping over the number of the elements in the list as an index to that list. My brief C experience many years ago was the reason for this. So, how to do it in Python... With enumerate() built-in function.

I can't understand what you mean with Perl-ism. I don't know Perl at all. Anyway, here it is. The Pythonic way. Pfff, sounds religious  Dodgy
>>> l = list(range(0, 50000, 5))

>>> for index, element in enumerate(l, start=1):
...     print("Item No. {} is {}.".format(index, element))
Output:
.................. .................. Item No. 9991 is 49950. Item No. 9992 is 49955. Item No. 9993 is 49960. Item No. 9994 is 49965. Item No. 9995 is 49970. Item No. 9996 is 49975. Item No. 9997 is 49980. Item No. 9998 is 49985. Item No. 9999 is 49990. Item No. 10000 is 49995.
The format() parameters are positional or key=value. The parameter can be an expression. The replacement of the parameters into the string is in the same order. But it is not necessary. 

>>> print("February in {1} has {0} days.".format(28, 2017))

February in 2017 has 28 days.

>>> print("February in {year} has {days} days.".format(days=28, year=2017))

February in 2017 has 28 days.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
Loops in List - by SandraDan - Mar-10-2017, 09:20 PM
RE: Loops in List - by wavic - Mar-10-2017, 09:30 PM
RE: Loops in List - by SandraDan - Mar-12-2017, 05:22 PM
RE: Loops in List - by Skaperen - Mar-11-2017, 08:04 AM
RE: Loops in List - by wavic - Mar-11-2017, 08:29 AM
RE: Loops in List - by Skaperen - Mar-12-2017, 04:43 AM
RE: Loops in List - by buran - Mar-11-2017, 08:50 AM
RE: Loops in List - by wavic - Mar-12-2017, 09:07 AM
RE: Loops in List - by ichabod801 - Mar-12-2017, 10:42 AM
RE: Loops in List - by Skaperen - Mar-13-2017, 03:08 AM
RE: Loops in List - by wavic - Mar-12-2017, 05:34 PM
RE: Loops in List - by wavic - Mar-13-2017, 07:42 AM
RE: Loops in List - by Skaperen - Mar-14-2017, 05:57 AM
RE: Loops in List - by ichabod801 - Mar-14-2017, 10:31 AM
RE: Loops in List - by Skaperen - Mar-15-2017, 05:13 AM
RE: Loops in List - by nilamo - Mar-20-2017, 04:07 AM
RE: Loops in List - by Skaperen - Mar-20-2017, 09:30 AM
RE: Loops in List - by snippsat - Mar-20-2017, 11:28 AM
RE: Loops in List - by Skaperen - Mar-21-2017, 02:32 AM
RE: Loops in List - by nilamo - Mar-21-2017, 02:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 952 Sep-18-2023, 07:52 AM
Last Post: buran
  Using recursion instead of for loops / list comprehension Drone4four 4 3,199 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Help with List Loops slackerman73 4 2,767 Nov-25-2019, 07:49 AM
Last Post: perfringo
  Adding items in a list (using loops?) Seneca260 6 2,817 Nov-22-2019, 11:34 AM
Last Post: Seneca260
  simple list check with loops Low_Ki_ 23 15,520 Jan-09-2017, 03:58 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020