Python Forum

Full Version: How can I return my list from a func?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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)
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
@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)]