-1

I want to replace the elements from list letters with the list elements from list animals where as print(letters) would print

["cat","dog","mouse","rabbit","hamster","duck","chicken","goose","hedgehog"]

How do I pass the value of the variable animals from the function newlist to the function main and then replace the value of variable in main

Code

def newlist():
    animals = ["cat","dog","mouse","rabbit","hamster","duck","chicken","goose","hedgehog"]
def main():
    letters = ["A","B","C","D","E","F","G","H","I"]
    print(letters)
main()  
Kitty
  • 125
  • 1
  • 2
  • 8
  • i'm trying to create an accounting program which gets lists and replace it with list elements from another list... i posted this much for simplicity that's all i need anyways, any help would be much appreciated.. – Kitty Apr 17 '16 at 12:47
  • i think it's very clear. i don't know how to rephrase it clearer. it's just replacing the elements on list `letters` with elements on `animals`. – Kitty Apr 17 '16 at 12:58
  • `letters = list(animals)` – miradulo Apr 17 '16 at 13:00
  • that would get an error like animals is not defined. it's from a different function. – Kitty Apr 17 '16 at 13:03
  • Your question is unclear and it does matter what you will be doing with the code. Many people commenting here know how to solve your problem but don't know what the problem is. – Selcuk Apr 17 '16 at 13:28
  • is saying, how to pass the value of the variable animals from the function newlist to main and then replace the value of variable main a clearer way to say it? – Kitty Apr 17 '16 at 13:38
  • sigh, answering questions with questions. – Kitty Apr 17 '16 at 13:42
  • maybe you need this http://stackoverflow.com/questions/19080943/how-to-return-a-value-from-a-function, hence create a function, set the letters to the result of this function? – Petter Friberg Apr 17 '16 at 14:40
  • 1
    Change `animals = ...` to `return ...` and then do `letters = newlist()` in `main()`. If you want to change the actual list object, use `letters[:] = newlist()` – zondo Apr 17 '16 at 14:43
  • thnx zondo! i was waiting for that answer. i appreciate your help. – Kitty Apr 17 '16 at 14:50

1 Answers1

3

Do note that functions in Python have to be called and they return specific values or None if nothing is specified.

From the Python Tutorial

The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.

Hence you need to define your function newlist as

def newlist():
     animals = ["cat","dog","mouse","rabbit","hamster","duck","chicken","goose","hedgehog"]
     return animals       # RETURN THE LIST

This will return the list as intended.

To get the list you need to call the function that can be done using newlist(). Hence your main function will look like

def main():
    letters = ["A","B","C","D","E","F","G","H","I"]
    letters = newlist()   # CALL THE FUNCTION 
    print(letters)

Now when you call main, you will get the intended output.


You do not need to create a separate list with placeholders. You can directly call newlist to get your list ready and working. In such a case the main function will be reduced to

def main():
    letters = newlist()   # newlist returns a list, so call directly 
    print(letters)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140