-3

I'm looking to create a function that takes a list and an integer as the argument and outputs the list in ascending order of very value that is less than the value in the second argument.

Then, if the value of integer (second arg) is lower than every value on the list, the function returns a print statement like, "There's nothing here."

def thisFunction(mylist=[], *myNum):
    print(mylist)
    print(*myNum)

    for x in list(int(mylist[0])):
        if x > myNum:
            mylist.remove(x)
            mylist.sort()
        elif x < myNum:
            print(f"There are no values less than numbers{myNum}")


thisFunction([12,4,5,6,7,11,56],5)

Right now, I get a TypeError: 'int' object is not iterable. I don't know what I'm doing wrong!

khelwood
  • 55,782
  • 14
  • 81
  • 108
hobey
  • 3
  • 3
  • 3
    What are you trying to accomplish with `list(int(mylist[0]))`? – blhsing Feb 10 '20 at 22:21
  • 3
    What was the problem with `def thisFunction(myList, myNum): ...`? – mkrieger1 Feb 10 '20 at 22:24
  • 1
    in `list(int(mylist[0]))` you try to do `list(12)` and it gives error. Why don't you use `for x in mylist:` – furas Feb 10 '20 at 22:47
  • 1
    BTW: it is not good idea to `remove()` element from list which you use with `for`. It may skip some elements. Better create new list with elements which you want to keep. – furas Feb 10 '20 at 22:48
  • Please share the entire error message. – AMC Feb 11 '20 at 01:38
  • I think this is a duplicate of https://stackoverflow.com/questions/19523563/python-typeerror-int-object-is-not-iterable – AMC Feb 11 '20 at 01:38
  • Thank you everyone for the questions -- I didn't realize the list(int(mylist[..]))) did not make any sense given that my argument entered a list in the first place. I've since amended the code thanks to JST99's answer. – hobey Feb 11 '20 at 19:55

1 Answers1

0

You can only apply list(args) when arg is an iterable, defined in the documentation as "An object capable of returning its members one at a time." Therefore, list(int(mylist[0])) doesn't make sense, as you are essentially attempting list(12) given the input list in your example.

Instead, perhaps what you want to do is to use the for loop to iterate through the given list, compare each element in that list with myNum, create a list containing only elements that are smaller than myNum, sort that created list, and print it. Here is the revised code to help you get started. (I made some stylistic edits to make the function more Pythonic and readable, i.e. replacing print calls in the function with a return statement, etc.)

def thisFunction(mylist, myNum):
    if myNum < min(mylist):
        return f"There are no values less than numbers{myNum}"
    else:
        lst = [num for num in mylist if num < myNum]
        lst.sort()
        return lst

We can check if there are any elements smaller than myNum in mylist by conveniently using the min function. If there are values smaller than myNum, we put these elements in a new list called lst and return it at its sorted state.

Check the output:

>>> print(thisFunction([12,4,5,6,7,11,56],10))
[4, 5, 6, 7]
>>> print(thisFunction([12,4,5,6,7,11,56],3))
There are no values less than 3
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jake Tae
  • 1,681
  • 1
  • 8
  • 11
  • Thank you so much for breaking this down for me. I searched how to pass a list into a function, but never got a clear answer for it. With your help, I was able to do it. Really appreciate it! – hobey Feb 11 '20 at 14:31
  • @hobey Glad it helped! – Jake Tae Feb 11 '20 at 17:07