0

It's a simple code practice challenge that asks that I make a function that takes a list of mixed types and returns only the integers

def return_only_integer(lst):
  for i in lst: 
    if type(i) != int: 
      lst.remove(i)

  return lst

That's it, it seems simple enough but the tests are coming back negative:

return_only_integer([9, 2, "space", "car", "lion", 16])

Returns: [9, 2, 'car', 16]

return_only_integer(["hello", 81, "basketball", 123, "fox"])

Returns what it should: [81, 123]

return_only_integer([10, "121", 56, 20, "car", 3, "lion"])

Also returns what it should: [10, 56, 20, 3]

but:

return_only_integer(["String",  True,  3.3,  1])

Returns: [True, 1]

The code is so simple and straightforward, I have no idea why these 2 tests are failing. Why would 'car' even be in the first list but the other strings not? type(True) is bool, why is it there?

Fabio Veronese
  • 7,726
  • 2
  • 18
  • 27
  • 3
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – jasonharper Sep 08 '20 at 03:23
  • instead of removing the items from the list, how about you create a new list with only integers and then return that? – Joe Ferndz Sep 08 '20 at 04:26

4 Answers4

0

This is probably due to you modifying the list in the conditional. By removing an item from the list, you are likely shifting the iteration in that operation.

It may be worth looking into filter() instead.

https://docs.python.org/3/library/functions.html#filter

0
def return_only_integer(lst):
  for i in lst: 
    if type(i) != int: 
      lst.remove(i)

  return lst

THIS FUNCTION HAS VERY BIG FAULT.

consider this list [9, 2, "space", "car", "lion", 16]

when it had removed "space" then your i had directly reached to lion and it ignored car because your list is changed and your i index is not changed. so it is moving as it is.

after removing one non integer, you must make sure to change the index position of i. so try this code. it will work.

def return_only_integer(lst):
  for i in lst:
    #print(i,type(i))
    
    if type(i) != int:
      #print("flag this is not integer ",i)
      lst.remove(i)
      return_only_integer(lst)

  return(lst)
print(return_only_integer(["hello", 81, "basketball", 123, "fox"]))

hope u understand. if you didn't understand then tell me .

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • @Abhinash, you shouldn't recommend an option where the program removes the item from the list while you are also iterating through the list using for loop. It is bad programming. – Joe Ferndz Sep 08 '20 at 04:36
  • Sir,I am not recommending it.I have just modified his code.Yes ,I know it is a bad programming.This idea of creating a temporary list within the function also came to my mind but I wanted give solution without much modifications .So he can do in his way. –  Sep 08 '20 at 06:20
  • Abinash, I really liked your solution. The creating a temp list is just the obvious way of doing it, I was intentionally trying for a different approach and couldn't figure out why that approach wasn't working. You answered that question, thank you – TechnoDiver Sep 08 '20 at 13:37
0

You can create a temporary list inside your function to hold the items that are integers. Once you have processed all the items, you can return the temporary list as part of your return statement. If there are no integers, you can return None.

def return_only_integer(lst):
  int_lst = []
  for i in lst: 
    if type(i) == int: 
      int_lst.append(i)
  return int_lst if int_lst else None

print (return_only_integer([9, 2, "space", "car", "lion", 16]))

print (return_only_integer(['ball', True, "space", "car", "lion", 'fish']))

This will output as follows:

[9, 2, 16]

None
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
0

Indeed you should not change the list you are iterating on, it produces unexpected results. Item deleted leaves room for the next, without the latter being picked in the iteration (so not being dropped in your example).

The possible choices to perform such task involve the usage of another list. A possible solution is very familiar to python developers:

def return_only_integer(lst):
  return [i for i in lst if type(i) == int]
Fabio Veronese
  • 7,726
  • 2
  • 18
  • 27