Ali
Ali

Reputation: 1383

How to return two values in Python

I have following code below.

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        splitName = name.split(' ')
        surname = splitName.pop()
        for i in range(len(splitName)):
            print('Name: %s' % splitName[i])

        return('Surname: %s' % surname)


np = NameParser()

print(np.getName("ali opcode goren"))

# output: name: ali, name: opcode, surname: goren

How do i return two values? Like following code:

for i in range(len(splitName)):
    return('Name: %s' % splitName[i])

return('Surname: %s' % surname)

# output: name ali: (error) i want all values name, name, surname

I want all values but just one output. How can I solve this problem?

Upvotes: 0

Views: 610

Answers (5)

samuel161
samuel161

Reputation: 281

or you can try

class test():
    map = {}
    for i in range(10):
        map[f'{i}'] = i
    return map

Upvotes: 0

Vivek Sable
Vivek Sable

Reputation: 10213

  1. Split: Split name by space and then do list comprehension again to remove the empty string from the list.
  2. POP: get last item from the list by pop() method and assign it to surname variable.
  3. Exception Handling: Do exception handling during pop process. If the input is empty then this will raise an IndexError exception.
  4. string concatenate: Iterate every Item from the list by for loop and assign the value to user_name variable.
  5. Concatenate surname in string again.
  6. Display result.

Demo:

class NameParser:
    def __init__(self):
        pass

    def getName(self, name):
        #- Spit name and again check for empty strings.
        splitName = [i.strip() for i in name.split(' ') if i.strip()]
        #- Get Surname. 
        try:
            surname = splitName.pop()
        except IndexError:
            print "Exception Name for processing in empty."
            return ""
        user_name = ""
        for i in splitName:
            user_name = "%s Name: %s,"%(user_name, i)
        user_name = user_name.strip()

        user_name = "%s Surname: %s"%(user_name, surname)
        return user_name


np = NameParser()
user_name = np.getName("ali      opcode       goren      abc")
print "user_name:", user_name

Output:

user_name: Name: ali, Name: opcode, Name: goren, Surname: abc

Upvotes: 3

Martijn van Wezel
Martijn van Wezel

Reputation: 1170

Try this:

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        listy = [] # where the needed output is put in
        splitName = name.split(' ')

        for i in range(len(splitName)):
            if i==(len(splitName)-1):#when the last word is reach
                listy.append('Surname: '+ splitName[i])
            else:
              listy.append('Name: '+ splitName[i])


        return listy


nr = NameParser()

print(nr.getName("ali opcode goren"))

# output: name: ali, name: opcode, surname: goren

whithout loop:

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        listy = [] # where the needed output is put in
        splitName = name.split(" ")
        listy ="Name",splitName[0],"Name",splitName[1],"Surname",splitName[2]



        return listy


nr = NameParser()

print(nr.getName("ali opcode goren"))

# output: name: ali, name: opcode, surname: goren

Upvotes: 1

joel goldstick
joel goldstick

Reputation: 4493

you can just do this:

def getName(self, name):
    return name.split(' ')

It will return a tuple

def get_name(name):
   return name.split(' ')

>>> get_name("First Middle Last")
['First', 'Middle', 'Last']

Upvotes: 1

ahmed
ahmed

Reputation: 5600

Try to use yield

class NameParser:

    def __init__(self):
        self.getName

    def getName(self, name):
        splitName = name.split(' ')
        surname = splitName.pop()
        for i in range(len(splitName)):
            yield ('Name: %s' % splitName[i])

        yield ('Surname: %s' % surname)


np = NameParser()

for i in (np.getName("ali opcode goren")):
    print i

Upvotes: 1

Related Questions