Python Forum

Full Version: List and isinstance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
(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.