Hello Group,
I am Rajiv, Python/Django developer in a startup, Bangalore. Today when I
experimenting with python I came across the following
CASE 1:
x = range(10)
[iter(x)] * 3
[listiterator object at 0x7f6aa5594850,
listiterator object at 0x7f6aa5594850,
listiterator object at
When you call iter(x) it returns you a listiterator object. Every time you call
iter(x) it *creates* a new listiterator and returns it back. The differences
you are seeing in your cases are not because of how lists of list operators
work, but because of how they are called.
In case-1, iter(x)
Hi Shreyas,
Thanks for your answer.
For the questions numered 2 and 3 do you have any thoughts?
2. Is there any other possibility or way (like * operator does here) by
which we can obtain the same result as in CASE 1?
3. Does only list and listiterators objects can behave this way? what other
Hi Rajiv,
Premise 1:
Every time you *create* an object, it has a new id. Examples:
a = list ()
b = list()
id(a), id(b)
(140688241027768, 140688241027840)
p = dict()
q = dict()
id(p), id(q)
(140688241071280, 140688241071560)
x = iter(range(10))
y = iter(range(10))
id(x), id(y)
2. I assume you want same result as case 1 - in case 2; case 3 is essentially
same as case 1 (which is why you are seeing one assignment 'changing' all three
lists). Well, instead of calling iter(x) three times, you need to call it only
once, and then use that object to multiply. In short, to
On Thu, Dec 11 2014, Rajiv Subramanian M wrote:
[...]
Question:
1. How in the first case i.e [iter(x)] * 3 creates a list of 3 but the
same objects?
The * operator when applied to a list (or any sequence type) is a
repetition. You can see the code for list_repeat here [1]. The line I've
Riptide IO http://riptideio.com/ has multiple openings for Python
programmers. We are looking for full-time Interns, entry-, mid- and
expert-level Python programmers. You could apply here -
https://www.linkedin.com/jobs2/view/13795011
We are also looking for Python developers in test -