user
user

Reputation: 7

list to tuple return in python

I am new to python. I am trying to create a function which takes string and list as arguments and returns a boolean value for every list element found (this should be returned as tuple) in the string. I have tried the following code

def my_check(str1, list1):
    words = str1.split()
    x = 1
    for l in range(len(list1)):
        for i in range(len(words)):
            if list1[l] == words[i]:
                x = x+1
        if (x > 1):
            print(True)
            x = 1
        else:
            print(False)

output = my_check('my name is ide3',  ['is',  'my', 'no'])
print(output)

This code outputs

True
True
False

How can i return this value as a tuple with

>>> output
(True,  True,  False)

Any idea is appreciated.

Upvotes: 0

Views: 754

Answers (4)

clocker
clocker

Reputation: 1366

You can check for a string directly within a string, so split() isn't necessary. So this works too:

def my_check(str1, list1):
    return tuple(w in mystr for w in mylist)
    # return [w in mystr for w in mylist] # Much faster than creating tuples

However, since returning a tuple as opposed to a new list isn't often needed, you should be able to just use straight list comprehension above (you can always cast the list to a tuple in your code downstream, if you have to).

python results:

In [117]: %timeit my_check_wtuple('my name is ide3',  ['is',  'my', 'no'])  
100000 loops, best of 3: 2.31 µs per loop

In [119]: %timeit my_check_wlist('my name is ide3',  ['is',  'my', 'no'])  
1000000 loops, best of 3: 614 ns per loop

Upvotes: 0

Yankai Zhang
Yankai Zhang

Reputation: 171

Considering the efficiency, maybe we should build a set from str1.split() first because query item in a set is much faster than that in a list, like this:

def my_check(str1, list1):
    #build a set from the list str1.split() first
    wordsSet=set(str1.split())
    #build a tuple from the boolean generator
    return tuple((word in wordsSet) for word in list1)

Upvotes: 0

abarnert
abarnert

Reputation: 365707

If you want to modify any code that prints things into code that returns things, you have to:

  1. Create an empty collection at the top.
  2. Replace every print call with a call that add the value to the collection instead.
  3. Return the collection.

So:

def my_check(str1, list1):
    result = () # create an empty collection
    words = str1.split()
    x = 1
    for l in range(len(list1)):
        for i in range(len(words)):
            if list1[l] == words[i]:
                x = x+1
        if (x > 1):
            result += (True,) # add the value instead of printing
            x = 1
        else:
            result += (False,) # add the value instead of printing
    return result # return the collection

This is a bit awkward with tuples, but it works. You might instead want to consider using a list, because that's less awkward (and you can always return tuple(result) at the end if you really need to convert it).

Upvotes: 1

Barry
Barry

Reputation: 302852

Generators to the rescue (edited: got it backwards the first time)

def my_check(str1, list1):
    return tuple(w in str1.split() for w in list1)

Upvotes: 0

Related Questions