Bottom Page

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?
Quote
#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)
snippsat and Mike Ru like this post
Quote
#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
buran and Mike Ru like this post
Quote
#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)]
Mike Ru likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  call func from dict mcmxl22 3 373 Jun-21-2019, 05:20 AM
Last Post: snippsat
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 701 May-29-2019, 02:27 AM
Last Post: heiner55
  About [from FILE import FUNC] Nwb 7 420 Apr-21-2019, 02:46 PM
Last Post: snippsat
  List creation and return in Function leoahum 3 324 Mar-04-2019, 04:45 PM
Last Post: leoahum
  Function to return modified list Pjones006 2 304 Feb-06-2019, 10:57 PM
Last Post: Pjones006
  Executing func() from a different folder ebolisa 2 380 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Making list empty after return in function dan789 10 937 Nov-24-2018, 11:54 PM
Last Post: micseydel
  Read list of IP addresses from file and return a network dflick 4 700 Oct-27-2018, 09:33 AM
Last Post: buran
  function 'return' and file content into a list shiro26 4 701 Jul-06-2018, 10:20 PM
Last Post: volcano63
  list func in lambda mepyyeti 1 994 Mar-10-2018, 09:07 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)