LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2017, 11:18 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Python - list empty after function


I'm new to python and attempting to learn python3.
I have a book that instructs

Quote:
Write a function called make_great() that modifies the list of magicians by adding the phrase the Great to each magicians name
I have written the below, but I can't make sense why after leaving the function, the list becomes []

Code:
#!/usr/bin/python3
magicians = ['useryeah', 'usercool', 'userno', 'duck']                          
print(magicians)                                                                
                                                                                
def make_great(magicians_list):                                                 
  great_magicians_list = []                                                     
  while magicians_list:                                                         
    great_magicians_name = 'the Great ' + magicians_list.pop()                  
    great_magicians_list.append(great_magicians_name.title())                   
    magicians = great_magicians_list                                            
  print(magicians)                                                              
                                                                                
                                                                                
make_great(magicians)                                                           
print(magicians)
I get this output
Code:
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
[]
but I expect

Code:
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
 
Old 03-18-2017, 01:01 AM   #2
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
You globally declared the variable and passed it to a function, which has it's own scope. You then referenced the variable in a print statement in the global scope without returning a value for the variable from the function scope.
It has no value because no value was passed back to the global scope from the functions scope after it was modified.
Local scopes can inherit from enclosing scopes, however they cannot set a value outside their own scope unless you do so explicitly in the enclosing scope.
Confused? No worries, it makes better sense if you read the code...

Code:
#!/usr/bin/python3
magicians = ['useryeah', 'usercool', 'userno', 'duck']                          
print(magicians)                                                                
                                                                                
def make_great(magicians_list):                                                 
  great_magicians_list = []                                                     
  while magicians_list:                                                         
    great_magicians_name = 'the Great ' + magicians_list.pop()                  
    great_magicians_list.append(great_magicians_name.title())                   
    magicians = great_magicians_list 
  print magicians                                         
  return magicians                                                              
                                                                                
                                                                                
magicians = make_great(magicians)
print(magicians)
Produces:
Quote:
bash-4.4$ python lq.py
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
This, on the other hand
Code:
...
print(make_great(magicians))
print(magicians)
Produces
Quote:
['useryeah', 'usercool', 'userno', 'duck']
['The Great Duck', 'The Great Userno', 'The Great Usercool', 'The Great Useryeah']
[]
Because in the second example I didn't specifically set the global variable magicians to the return value, I just printed it.
Scoping can be a rough spot, everyone has to puzzle it out.
Last suggestion:
A better way to do this is list comprehension
Code:
#!/usr/bin/python3
magicians = ['useryeah', 'usercool', 'userno', 'duck']                          
print(magicians)                                                                
                                                                                
def make_great():                                                 
  return [ 'The Great ' + x for x in magicians]                                                               
                                                                                                                                                               
magicians = make_great()
print(magicians)
Just remember, Python is extremely cool, and you are cool for learning it

Last edited by dijetlo; 03-18-2017 at 02:12 AM. Reason: More accurately answers the OP question
 
2 members found this post helpful.
Old 03-18-2017, 09:34 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
This helped tremendously!
I am reminded of bash and subprocesses when it comes to scoping.
For example,

Code:
$ a=hello; (a=goodbye); echo "$a"
hello
So I presume a analogy of return is it "escapes" the current confined scope of a function into the global scope?
I had not read yet about return (I think) but I'll test around with it

Also, I had read about list comprehension, but this made me realize how it could be relevant
I will have to test with it more too.

Thank you!
 
Old 03-18-2017, 09:52 PM   #4
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
Quote:
Also, I had read about list comprehension, but this made me realize how it could be relevant
I will have to test with it more too.

Let me know if I can be of further help.
 
Old 03-19-2017, 03:12 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569
I know this wasn't the question, but I wanted to comment on your loops.

The Pythonic way to get that modified list would be with a list comprehension:

Code:
great_magicians = [m + " the great" for m in magicians]
Or with Python 3.6:

Code:
great_magicians = [f"{m} the great" for m in magicians]
A foreach loop is another common option:

Code:
great_magicians = []
for m in magicians:
    great_magicians_list.append(m + " the great")
A map function is one that I don't see much (list comprehensions are better), but it's also supported:

Code:
great_magicians = list(map(lambda m: m + ' the great', magicians))

Last edited by dugan; 03-19-2017 at 03:21 PM.
 
2 members found this post helpful.
Old 03-19-2017, 09:17 PM   #6
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
Eh, Sefyirs while loop ran without issue, which is pythonic enough for a beginner. His problem was scoping but he seems to have gotten a handle on that. Let's not confuse the young Jedi unnecessarily, he's just putting his light sabre together. He'll come to the pythonic force on his own, given time (assuming he doesn't choose the dark side, aka Ruby)
We should probably start a 'Stupid Python Tricks' thread if we want to get into optimizations and machine learning atomics.
On that note, Dugan, do you have any background with anaconda (the scipy environment) and if you do, can you recommend a tutorial ? I'm just getting started with it and it looks like Guidos swiss army knife (a little confusing for a mere mortal)

Last edited by dijetlo; 03-19-2017 at 09:30 PM.
 
1 members found this post helpful.
Old 03-20-2017, 09:14 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569
Quote:
Originally Posted by dijetlo View Post
On that note, Dugan, do you have any background with anaconda (the scipy environment) and if you do, can you recommend a tutorial ?
I don't, sorry.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: why threading.thread pass empty dictionary to function thread golden_boy615 Programming 0 03-31-2014 11:45 AM
Hosts list empty kususe Linux - Networking 2 11-30-2010 05:31 PM
Action list for attachments in Thunderbird is empty integrale16 Slackware 1 11-18-2009 04:27 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
vsftpd login only diplays empty list michaeltweak SUSE / openSUSE 1 02-18-2007 04:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration