Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop + add something
#10
(Jun-23-2022, 11:04 PM)Pedroski55 Wrote: Knowing a list is in fact an integer indexed array, the index beginning with zero, why not make use of that fact and the relationship of the length to the indexing? Simple and useful!
No,it's just stupid if think about it.
Old but good Loop Like A Native
Quote:Python gives us a much more natural way to loop over the values in my_list.
Why are we talking about integers? This is like a Rube Goldberg machine.
We set up a mechanism to give us integers, when we don’t want the integers at all,
so the first thing we do with each integer is turn it into a list element.
We might has well have written,the boot kicks the ball into the net, which tips the watering can...
Rather than iterating over indexes, and using the index i to get the value we really want from the list, we can simply loop over the values directly
# The Rube Goldberg machine way
cars = ["Ford", "Volvo", "BMW"]
for car in range(len(cars)):
    print(f"The car {cars[car]} has been sold")
Output:
The car Ford has been sold The car Volvo has been sold The car BMW has been sold

# Simple as this
cars = ["Ford", "Volvo", "BMW"]
for car in cars:
    print(f"The car {car} has been sold")
Output:
The car Ford has been sold The car Volvo has been sold The car BMW has been sold
Reply


Messages In This Thread
For loop + add something - by Matheus - Jun-23-2022, 07:34 PM
RE: For loop + add something - by Axel_Erfurt - Jun-23-2022, 07:43 PM
RE: For loop + add something - by Matheus - Jun-23-2022, 09:12 PM
RE: For loop + add something - by Matheus - Jun-23-2022, 09:06 PM
RE: For loop + add something - by BashBedlam - Jun-23-2022, 09:16 PM
RE: For loop + add something - by snippsat - Jun-23-2022, 09:19 PM
RE: For loop + add something - by Matheus - Jun-23-2022, 09:37 PM
RE: For loop + add something - by DeaD_EyE - Jun-23-2022, 10:18 PM
RE: For loop + add something - by Pedroski55 - Jun-23-2022, 11:04 PM
RE: For loop + add something - by snippsat - Jun-24-2022, 01:03 AM
RE: For loop + add something - by DeaD_EyE - Jun-24-2022, 08:26 AM

Forum Jump:

User Panel Messages

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