Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python range() and zip() object type

People also ask

What is a zip object Python?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

Why do you use the zip () method in Python?

Python's zip() function creates an iterator that will aggregate elements from two or more iterables. You can use the resulting iterator to quickly and consistently solve common programming problems, like creating dictionaries.

How do you read a zip object in Python?

The zip() function returns an iterator of tuples based on the iterable objects. If a single iterable is passed, zip() returns an iterator of tuples with each tuple having only one element. If multiple iterables are passed, zip() returns an iterator of tuples with each tuple having elements from all the iterables.

How do you zip a string and a list in Python?

Python zip() Method. The zip() method takes one or more iterables (such as list, tuple, string, etc.) and constructs the iterator of tuples where each tuple contains elements from each iterable.


You must be using Python 3.

In Python 2, the objects zip and range did behave as you were suggesting, returning lists. They were changed to generator-like objects which produce the elements on demand rather than expand an entire list into memory. One advantage was greater efficiency in their typical use-cases (e.g. iterating over them).

The "lazy" versions also exist in Python 2.x, but they have different names i.e. xrange and itertools.izip.

To retrieve all the output at once into a familiar list object, you may simply call list to iterate and consume the contents:

>>> list(range(3))
[0, 1, 2]
>>> list(zip(range(3), 'abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]

In Python 3.x., range returns a range object instead of a list like it did in Python 2.x. Similarly, zip now returns a zip object instead of a list.

To get these objects as lists, put them in list:

>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> zip('abc', 'abc')
<zip object at 0x01DB7120>
>>> list(zip('abc', 'abc'))
[('a', 'a'), ('b', 'b'), ('c', 'c')]
>>>

While it may seem unhelpful at first, this change in the behavior of range and zip actually increases efficiency. This is because the zip and range objects produce items as they are needed, instead of creating a list to hold them all at once. Doing so saves on memory consumption and improves operation speed.


Range (xrange in python 2.*) objects are immutable sequences, while zip (itertools.izip repectively) is a generator object. To make a list from a generator or a sequence, simply cast to list. For example:

>>> x = range(10) 
>>> list(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

But they differ in a way how elements are generated. Normal generators are mutable and exaust their content if iterated, while range is immutable, and don't:

>>> list(x) # x is a range-object
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # second cast to list, contents is the same
>>> y = zip('abc', 'abc')
>>> list(y) # y is a generator
[('a', 'a'), ('b', 'b'), ('c', 'c')]
>>> list(y) 
[] # second cast to list, content is empty!