Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop + add something
#8
(Jun-23-2022, 09:37 PM)Matheus Wrote: Thanks. How come not to use "len" in that way?
There are objects which does not have knowledge about len, but are iterable. For example, iterating over a file-object. As a result, you get line by line, but the iterator does not know how many lines, so the len function could not work on a file object.

All sequences (bytes, str, tuple, list), all collections (set, frozenset) and mappings (dict), Generators and Iterators support iteration, but not all share the same behavior of index access. On collections, you can't get the 10th element. You can get 10 unique elements from a set in a "random" order with the pop method or with iteration.

All share the same, all support iteration.
If you have a list with cars and want to print the cars, then iterate over cars.
If you want to count during iteration, then use enumerate. It takes an iterable and the second argument is the start.

Iteration is strong in Python and used in many places. Things like range(len(something)) are antipatterns because you take the flexibility away.

Bad example (cars is now a set):
# cars is now a set
# a set has unique elements and no order
# no index access
# but you can still iterate over them
cars = {"Ford", "Volvo", "BMW","test"}
 

# this part has not been changed
for r in range(len(cars)):
    print(f"The car {cars[r]} has been sold")
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [1], in <cell line: 5>() 4 # this part has not been changed 5 for r in range(len(cars)): ----> 6 print(f"The car {cars[r]} has been sold") TypeError: 'set' object is not subscriptable
A set has no order, so there is no index access.
(Maybe in future)


Pythonic way:
# natural words which explain the meaning
unique_cars = {"Ford", "Volvo", "BMW"}

for car in unique_cars:
    print(f"The car {car} has been sold")
The car BMW has been sold
The car Volvo has been sold
The car Ford has been sold
No Exception.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
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