Is it possible to loop through a list and feed the values to a functio

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1323 Views - Last Post: 22 February 2018 - 10:38 PM Rate Topic: -----

#1 fad3r   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 01:19 PM

I have a list and a function and I am trying to loop through list and pass those values to the function but when I do and try to print the result it doenst seem to do anything.

#Inputs
b = " is awesome"
c = 10

input_list = ['mike', 1, 'leon', 2, 'david', 3]

#Function
def listed(a):
    if "str" in str(type(a)):
        d = a + b
        return (d)
    else:
        e = a + c
        return (e)
print(listed)


for x in input_list:
    listed(x)




I also appear to be having battles (again) with my arch nemesis return. When I try to print d or e outside the function I get told they are defined. I thought that was the whole point of returning them.

Any help on what I am confusing on return and how to feed the input to a list appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to loop through a list and feed the values to a functio

#2 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 01:24 PM

When you return a value from a function, it's returned to the place you call the function, i.e. line 19. You're just throwing the return value away there. You need to do your printing there.

Line 15 is also wrong: listed is a function and you're trying to print it. All that's going to do is give you the string representation of that function (something like what it's name is and where it resides in memory), which isn't what you want. You want to print the value returned by the function.

This post has been edited by ndc85430: 13 February 2018 - 01:37 PM

Was This Post Helpful? 2
  • +
  • -

#3 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 01:39 PM

It seems like you're able to understand how returning works, given what you've written on line 9:

str(type(a))


What do you think is going on there?
Was This Post Helpful? 3
  • +
  • -

#4 fad3r   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 01:51 PM

In that line what we are doing is making a string of a type. I am really confused on return. I think it has to do with not totally getting scoping and how functions work. I don't have a CS background I am jsut teaching myself through a variety of places. This is clearly a weakness because it comes up again and again.
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 01:58 PM

View Postfad3r, on 13 February 2018 - 08:51 PM, said:

In that line what we are doing is making a string of a type.


Yes, but type is a function that returns a value that is then passed to str. Functions basically have an input and an output (well not always, but this is enough for now). Using return is how you get the output.

str(type(a))


is the same as

type_of_a = type(a)
str(type_of_a)


On line 1 here, a is passed to type and the return value is stored in type_of_a. type_of_a is then passed to str (and here I'm throwing the return value away, but I wanted to focus on returning on the first line).
Was This Post Helpful? 1
  • +
  • -

#6 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:08 PM

How are you learning the language? Perhaps Allen Downey's Think Python would help.
Was This Post Helpful? 0
  • +
  • -

#7 fad3r   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:12 PM

Ok that all makes sense to me. I understand that. I start to lose things when functions get involved because then I need return. Like I understand I have to add return but then I get confused on how to call the variable I am returning outside the function.

Like I understand (on like a 3rd grade level at best) that this has to do with scope and that the variable created in the function is local. I understand by returning it we should be able to use it outisde but I can't figure out how to access it or use it outside.

I really appreciate the hand holding btw.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:13 PM

Can you see how it works in the example I've given you in post 5?
Was This Post Helpful? 0
  • +
  • -

#9 fad3r   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:13 PM

A friend is giving me challenges and I am watching videos and using Datacamp.com

I also have like 200 books a friend gave me so I look stuff up and then I use the internet on top of it. Right now I am reading through learning python by o'reilly but now I am going to go look this up in a dummies book.
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:30 PM

View Postfad3r, on 13 February 2018 - 09:12 PM, said:

but then I get confused on how to call the variable I am returning outside the function.


I think this is what's causing confusion. You're not returning a variable from the function, you're returning the value that's assigned to that variable. The variable itself is scoped to the function and therefore can't be accessed outside.

Consider the following:

def square(x):
    squared = x ** 2
    return squared

y = square(4)
print(squared) # No, this won't work



Here, y will have the value 16, since that's the value that's returned by the function call square(4). Again, there's no access to the variable squared since it is only accessible inside the function. Again, the function returns the value that squared referred to.

I could write the function in the following way, which perhaps makes the last point clearer:

def square(x):
    return x ** 2


Was This Post Helpful? 1
  • +
  • -

#11 fad3r   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:44 PM

I sort of see. Like it is hazy but not like smog where I can't see through it.

I can print y.

So do we always have to assign new variables to functions to get the value out of the return that happened in the function?

So in mine I would try xyz=listed and then print listed is the next step I think.
Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:46 PM

Well no, you can pass the value directly to another function without assigning it to a variable (like you're doing with str(type(a)). When you do assign to a new variable, of course you can use the same name as one that's inside the function, i.e.

squared = square(4)


squared here is not the same variable as the one inside the function square - there are two different scopes.
Was This Post Helpful? 1
  • +
  • -

#13 fad3r   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 02:51 PM

Ok that works.

With this code. What is considered the "caller"


b = " is awesome"
c = 10

input_list = ['mike', 1, 'leon', 2, 'david', 3]

#Function
def listed(a):
    if "str" in str(type(a)):
        d = a + b
        return (d)
    else:
        e = a + c
        return (e)


leon = listed("test")
print (leon)



This post has been edited by fad3r: 13 February 2018 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#14 JustaNoOb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 18

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 03:24 PM

View Postfad3r, on 13 February 2018 - 02:44 PM, said:

So do we always have to assign new variables to functions to get the value out of the return that happened in the function?


No, in ndc85430 example
y = square(4)
print(y)
print(square(4))

Both ways would work.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,911
  • Joined: 19-March 11

Re: Is it possible to loop through a list and feed the values to a functio

Posted 13 February 2018 - 06:33 PM

View Postfad3r, on 13 February 2018 - 04:44 PM, said:

So do we always have to assign new variables to functions to get the value out of the return that happened in the function?


No. The function call is an expression that returns a value*. You can treat that expression as you would any value: you can store it to a variable, you can pass it to another function, or you can use it as a step in some other computation. The important thing here is the concept of an expression. Try to come up with other examples of expressions, and see if you can convince yourself that they're all equivalent in python, syntactically speaking.
Also, you might want to think a little bit about the difference between an "expression" and a "statement". These are two important concepts in understanding most languages, and I think it'll help you get an idea of what all is going on here.

Quote

So in mine I would try xyz=listed and then print listed is the next step I think.


No. Other way around.

* it always returns a value. if there is no explicit return statement, it returns None. you should write code to test this assertion, it'd be good practice for you. You'll have to figure out how to tell whether a given value is None, but I think you might be able to figure that out if you read this carefully.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2