Python Forum
How can I return my list from a func?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I return my list from a func?
#1
I have a list. I'm getting items that starting from number.
s = ['kdkkd', '1 america', 'wwww', '2 Russia', 'd333', '3England']
def get_title_list(a):
    for x in mylist:
        try:
            int(x[0])
            print(x)
        except ValueError:
            pass
get_title_list(s)
It works, but it doesn't work if I'm trying like this
s = ['kdkkd', '1 america', 'wwww', '2 Russia', 'd333', '3England']
def get_title_list(a):
    for x in mylist:
        try:
            int(x[0])
            return x
        except ValueError:
            pass
print(get_title_list(s))
I get one itme from s. How to return?
Reply
#2
you need to construct the filtered list before returning from function. At the moment you return after first item that starts with digit.
s = ['kdkkd', '1 america', 'wwww', '2 Russia', 'd333', '3England']

def get_title_list(my_list):
    filtered = []
    for item in my_list:
        try:
            int(item[0])
            filtered.append(item)
        except ValueError:
            pass
    return filtered
print(get_title_list(s))
This is your code with minimal changes. Here are some more examples. Even your code can be changed to not use int conversion.

using comprehension

import string
def get_title_list(my_list):
    return [item for item in my_list if item[0] in string.digits]
using filter
import string
def get_title_list(my_list):
    return filter(lambda x: x[0] in string.digits, my_list)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Another way is to yield instead of return.
Then make it list() outside of function or can also iterate over it.
def get_title(lst):
    for x in lst:
        try:
            int(x[0])
            yield x
        except ValueError:
            pass

if __name__ == '__main__':
    lst = ['kdkkd', '1 america', 'wwww', '2 Russia', 'd333', '3England']
    print(list(get_title(lst)))
Output:
['1 america', '2 Russia', '3England']
>>> for item in get_title(lst):
...     print(item)
...     
1 america
2 Russia
3England
Reply
#4
@snippsat post actually reminded me that in python3 filter would return filter object, so if you want a list you need to explicitly convert it

import string
def get_title_list(my_list):
    return list(filter(lambda x: x[0] in string.digits, my_list))
or you need to iterate over like in snippsat's example
s = ['kdkkd', '1 america', 'wwww', '2 Russia', 'd333', '3England']
import string
def get_title_list(my_list):
    return filter(lambda x: x[0] in string.digits, my_list)

for item in get_title_list(s):
    print(item)

further on, because of recent discussion on PEP8 compliance and because PEP8 does recommend using str.starstwith method instead of slicing/indexing when check for prefix (same for str.endswith and checking for suffix). e.g.

import string
digits =  tuple(string.digits)
def get_title_list(my_list):
    return [item for item in my_list if item.startswith(digits)]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Entry field random pull from list, each return own line Bear1981 6 832 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  what to return for an empty list Skaperen 2 1,217 May-24-2024, 05:17 PM
Last Post: Skaperen
  How can I print from within actor(func) ?? Pedroski55 2 943 May-01-2024, 05:35 AM
Last Post: Pedroski55
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 21,086 Jan-05-2024, 08:30 PM
Last Post: sgrey
  How to output one value per request of the CSV and print it in another func? Student44 3 2,692 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Need to parse a list of boolean columns inside a list and return true values Python84 4 3,240 Jan-09-2022, 02:39 AM
Last Post: Python84
  How to invoke a function with return statement in list comprehension? maiya 4 4,060 Jul-17-2021, 04:30 PM
Last Post: maiya
  Regular expression: return string, not list Pavel_47 3 3,386 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Func Animation not displaying my live graph jotalo 0 1,977 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 6,063 Oct-24-2020, 03:41 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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