Python Forum
List and isinstance - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List and isinstance (/thread-15356.html)



List and isinstance - Roh_80 - Jan-14-2019

numbers = [1, 2, 3,[4,5,6]]`
for i in numbers:`
	if isinstance(i,list):`
		`print("YES")`		
	else:
		print("NO")`
1. when 1 = 1 , why it ins't an instance of list ? It is a nested list and 1 is definitely an element of outer list.
2. When i becomes 4 , output is YES. It is an inner list so why the same logic was not implemented for when i = 1.


RE: List and isinstance - ichabod801 - Jan-14-2019

(Jan-14-2019, 03:48 PM)Roh_80 Wrote: when 1 = 1

This is why you shouldn't name variables 'i', because it easily gets confused with 1 or l.

The first time through the loop, i is 1. That is not a list, that is an integer. It doesn't matter if it is an element of a list, isinstance checks if it is a list itself. The last time through the list, i is [4, 5, 6]. That is a list, and it is a list of integers.