Python Forum
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.
Reply
#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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,096 Aug-10-2021, 08:10 AM
Last Post: snippsat
  basic question isinstance tames 5 2,773 Nov-23-2020, 07:20 AM
Last Post: tames
  Help with isinstance command (very simple code) Laplace12 2 1,967 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  isinstance() always return true for object type check Yoki91 2 2,503 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Class isinstance not recognizing Windspar 3 5,835 Dec-08-2017, 06:13 PM
Last Post: Windspar
  isinstance not outputting anything mikerosz94 3 3,378 Aug-31-2017, 03:07 PM
Last Post: mikerosz94
  Repost isinstance not outputting any statements mikerosz94 1 2,884 Aug-31-2017, 12:46 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020