What is the difference between iterable and sequence?

I can't understand the difference between the concepts of "iterable" and "sequence" in Python. Please explain.

Author: insolor, 2019-09-19

1 answers

An iterable object (iterable) allows you to view its elements sequentially. But you may not be able to access an arbitrary element.

A sequence (sequence) is an iterable object whose elements can be accessed by an integer index, and you can also find out the total number of elements (the length of the sequence).

Examples:

  • list, tuple, str - this is sequences
  • functions-generators, file objects, objects zip, iter, map etc. - iterable objects
  • dict - is an iterable object, but is not a sequence (accessing values not by an integer index, but by a key, which can be any hashed object), if more specifically-refers to mapping objects.
 13
Author: insolor, 2020-02-12 06:25:13