Bottom Page

Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List and isinstance
#1
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.
You like this post
Quote
#2
(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.
You like this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Class isinstance not recognizing Windspar 3 749 Dec-08-2017, 06:13 PM
Last Post: Windspar
  isinstance not outputting anything mikerosz94 3 857 Aug-31-2017, 03:07 PM
Last Post: mikerosz94
  Repost isinstance not outputting any statements mikerosz94 1 641 Aug-31-2017, 12:46 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)