2

This code is only printing 1 2 4 5..My question is that why p is not updated with the new array at 3rd iteration

p = [1, 2, [1, 2, 3], 4, 5]
for each in p:
  if type(each) == int:
    print each
  else:
    p = each

Actually to be precise when debugged the code I saw that it actually updating the value of p but each variable is not reinitialised again.

Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38

2 Answers2

9

Because of if type(each) == int: line. Your third element is a list ([1, 2, 3]) and not an int, so it doesn't print anything.

Now what comes to changing the p variable: p is just a name for an object, not the object itself. If you do p = each inside the for loop, it doesn't affect the original object you're looping through, it just changes the name p to a local name, which points to a different object. As soon as that round of the loop ends, your for loop continues to do its business with the original object you were looping through.

So, notice that p = each doesn't change the existing object (the p you're looping through), it simply creates a new local name p which points to the value of each.


What you most likely want is something like this:

p = [1, 2, [1, 2, 3], 4, 5]
for each in p:
    if isinstance(each, list):
        for x in each:
            print x
    else:
        print each

This then again, this isn't recursive, and you'd need a function for that:

def print_elements(iterable):
    for element in iterable:
        if isinstance(element, list):
            print_elements(element)
        else:
            print element

If you want to unpack the values into one list to use them for something other than printing, you should use something like this:

def recursive_unpack(iterable):
    for element in iterable:
        if isinstance(element, list):
            yield from recursive_unpack(element)
        else:
            yield element

Why I'm using isinstance() instead of type(): Differences between isinstance() and type() in python

Also, if you want this to apply to all iterables (my last example) and not just lists: In Python, how do I determine if an object is iterable?

Community
  • 1
  • 1
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
  • Thanks man I completely understood this...And as a matter of fact I already solved it using recursion ..But still thanks.. – Mayukh Sarkar Jun 18 '15 at 18:54
  • @MayukhSarkar If you think you have a better solution, post it here as an answer for everyone to see. If this is your solution, or a better one, please accept my answer as the correct solution :) – Markus Meskanen Jun 18 '15 at 18:55
  • But the without recursion solution will not solve multiple nested list..and the with recursion solution is same as mine – Mayukh Sarkar Jun 18 '15 at 18:58
  • Yes recursion is the correct way to to do this..but it has some serious memory overhead..all those multiple stack push & pops..Moreover think about a situation where on a limited ARM processor ram, to perform some operation, I want to implement this..All those recursive levels create a hell lot of memory & time overload – Mayukh Sarkar Jun 18 '15 at 19:05
  • @MayukhSarkar You should accept my solution if it's, there's this little tickmark under the up- and downvote arrows. And what comes to your recursion memory issue, that's a whole different question for you to ask on this site. Your original question was about why isn't the indented list getting printed, and now you're asking about memory optimization. You really should create a new question :) – Markus Meskanen Jun 18 '15 at 19:07
  • I accepted your answer..Sorry I am a new member & hence I didn't know this – Mayukh Sarkar Jun 18 '15 at 19:12
0

the issue is in the else statement, you're basically saying that if the each is not an int (in your case it's a list) then set the list p to the inside list.

I think that what you're trying to do can be accomplished by something like

p = [1, 2, [1, 2, 3], 4, 5]
for element in p:
if type(element) == int:
    print element
else:
    for otherElement in element:
        if type(otherElement) == int:
            print otherElement

the else statement in this case goes through the inside list and checks the elements it contains (otherElement)