Sobes.tech
Junior — Middle

What data categories in Python allow element-wise iteration?

sobes.tech AI

Answer from AI

In Python, the following data categories support element-by-element iteration:

  • Lists (list) — ordered mutable collections.
  • Tuples (tuple) — ordered immutable collections.
  • Strings (str) — sequences of characters.
  • Sets (set) — unordered collections of unique elements.
  • Dictionaries (dict) — collections of key-value pairs, iterated over keys.
  • Generators and iterators — objects that return elements one by one during iteration.

Example of iterating over a list:

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

Thus, almost all built-in collections and objects implementing the iterator protocol allow element-by-element traversal of data.

What data categories in Python allow element-wise… - sobes.tech