Change language

How to Fix TypeError: NoneType Object is not iterable

How to Fix TypeError: NoneType Object is not iterable

hi everybody its Joe here Im a data engineer  working here in Dublin Ireland um so welcome to the channel its your first time, we do weekly  blog posts video tutorials and live streams and everything and anything data analytics. Today were going to  talk about uh Type Error non-type object its not iterable im going to take you to how to reproduce  the problem explain what the what it actually how it is and how it occurs and then we can fix it  so lets hop over to the program have a look at it and lets see in a second right so uh today  were going to talk about type error non-type is not issuable so i suppose to get to the bottom  of this um first of all we need to understand a couple of things before you can actually  understand what the area is and how it occurs so when youre working with data obviously working  with data types theres different data types and what you can have is you can have values  that are integers or values that are empty and non-type is usually so well is is  associated with values that are empty so essentially when you think about  the data type non-type you could say well you could count something and somebody could  ask you how much did you count there it has zero so what fizzly would say on a database table  if you had a column for that you would say zero but then separately somebody could say count  for but you didnt do anything so essentially whats what youre returning is nothing empty an  empty value because you counted nothing youre not actually returning the value and thats where  a num type comes in so a non-type is essentially anything thats empty now theoretically none  type and uh say an integer or value zero are thoroughly the same but theyre not um in  the computing world in a programming world what they may represent in your mind and logically  the same thing theyre actually two different things and theyre actually two different data  types so today were going to do is just talk you through the this problem when you come across  us how you can fix it and how you can recognize and how you can fix it so on the screen here um  what we have is a value called a a variable okay what weve done is weve valued weve actually  assigned it the value um assigned to a to the value known okay so essentially when we ran this  logic right what happened was if you can see it here down in the left-hand corner im just going  to highlight it here first of all to give up the error okay its the error trying to fix well go  through that in a second but it actually says that uh none a is actually a days hype non-type so  when youre actually trying to loop over something if its empty and it has nothing um this problem  here to watch this video for will actually throw up the error so here um we have a loop right so  we know that a is none and no data type means basically means its empty so you cant loop you  cant loop over anything that has no values in it and essentially thats what the crux of this whole  issue is if you get this error type error none type object is not iterable you will essentially  get um this error because its empty so here what weve done as i said weve basically created  the value a assigned a non data type which is basically nothing and then weve printed it and  then weve tried to loop over so were basically saying for i and a which is this value here  which is empty print i so thats why logically you would guess you cant loop over anything  thats an empty value um dash basis theres nothing in it so if we change this we could also  we could fix this in a number of ways right so one way you could do it is if you didnt want  to return non-type you could actually assign a to a string to say zero okay so if we did this  okay and run okay it would give you back class of string and value zero so essentially if you  didnt want to get this error and you wanted to fix it one way to do it is just assign the  variable that youre trying to loop over a value of zero put in and close commas um  another way you could do it um is you could create a list right so lets just do this say  zero one so two so three okay and we do this okay and what that does is actually its  assigned a to a list and now and so thats your list here and theres your class it shows  it there and its basically obviously printed out and then weve looped weve looped for  each value we basically printed it out here so essentially um in a nutshell when youre  getting this value its always important to check that when youre looping youre getting this  value what has been passed in and its returning any values and are the values actually is there a  physical value what you need is the physical value if youre not getting um if youre not getting  any values in this return and the num type empty youre going to get this error when youre looping  so the best thing to do is do what i did here is just basically assign values you could change that  0 1 2 3 to just anything like 0 0 0s anything like that anything is a value all its the program  is doing is its looking for a value and once it finds it it bases i can loop over that and i  can return the values and no errors will occur so thats how you fix type error non-type is not  iterable i hope you enjoyed this video today i hope youve got uh learned from us and i hope  youve basically been able to solve any problems you may have in your data analytics project so  as i said at the start we do weekly data project sorry we do weekly um live post blog posts uh  live streams and video tutorials on everything in data linux uh please like subscribe trying  to build the channel trying to hit thousand subscribers at the moment we really appreciate  your subscription please share with your friends uh colleagues or anything you might anybody might  see im interested in this we do post weekly videos so um well be doing more soon and more  trying to try to make it as relevant as it can so as i said like and subscribe and well see  you soon take care and look after yourself you