How can I efficiently capitalize items in a list without modifying the original?

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Beginner Python
Click For Summary

Discussion Overview

The discussion revolves around writing a Python function that capitalizes items in a list of strings without modifying the original list. Participants explore different coding approaches, clarify concepts related to variable scope, and suggest optimizations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant presents an initial function definition and questions the need to define a new list outside the function.
  • Another participant discusses variable scope, suggesting that local variables are not accessible outside the function.
  • Several participants confirm that the function works when the returned value is assigned to a variable.
  • One participant proposes using list comprehensions or the map function as potential optimizations.
  • Another participant suggests an alternative method using the list's index method, but acknowledges that it would modify the original list.
  • Participants express uncertainty about the implications of using different Python versions on their code.

Areas of Agreement / Disagreement

Participants generally agree on the need to assign the function's output to a variable. However, there are multiple competing views regarding the best approach to capitalize the items without modifying the original list, and the discussion remains unresolved on the most efficient method.

Contextual Notes

Some participants mention the potential inefficiency of certain methods, but no consensus is reached on the optimal solution. The discussion also reflects varying levels of Python knowledge among participants.

Who May Find This Useful

Readers interested in Python programming, particularly those learning about functions, variable scope, and list manipulation techniques.

zeion
Messages
455
Reaction score
1

Homework Statement



I'm supposed to write a function that does this:

def capitalize_items_2(lst):
Return a new list with the same items as lst, only in upper case. lst is a list of strings. This function does not change lst

Homework Equations





The Attempt at a Solution




def capitalize_items_2(lst):
lst2=[]
i=0
for x in lst:
lst2.append(lst.upper())
i+=1
return lst2


But how come I can't define lst2 inside the function and return it?
Do I need to define it outside or is there a better way?
Thanks.
 
Technology news on Phys.org
My Python knowledge is very limited, but I hope I can help.
If you are unable to return a list defined within a function, then you are conflicting with the scope of variables inside of functions. What is the lifetime of lst2 in the function? That is, do local variables to the function 'die' when the function ends? If so, then you can't return a variable that no longer exists.

You may want to look into list comprehensions at http://docs.python.org/tutorial/datastructures.html
or the map function.
 
zeion said:
Code:
def capitalize_items_2(lst):
    lst2=[]
    i=0
    for x in lst:
        lst2.append(lst[i].upper())
        i+=1
    return lst2

I was able to successfully run your code. (There's also some things you can do to optimize your code.)
 
jhae2.718 said:
I was able to successfully run your code.

Weird..

I tried to actually test it on a list like this:

def capitalize_items_2(lst):
i=0
lst2=[]
for x in lst:
lst2.append(lst.upper())
i+=1
return lst2

lst = ["fs", "trt", "fdscv"]
capitalize_items_2(lst)
print lst2

I get this:

Traceback (most recent call last):
File "C:\Documents and Settings\Truman\Desktop\cap2", line 12, in <module>
print lst2
NameError: name 'lst2' is not defined

It works if I define lst2 outside the def though.

Does it matter what version I'm using?
 
How do you call the function? You need to store the returned value in a variable, like this:
Code:
outputList = capitalize_items_2(inputList)
where inputList is the function input and outputList stores the result of the function.
 
zeion said:
Weird..

I tried to actually test it on a list like this:

Code:
def capitalize_items_2(lst):
    i=0
    lst2=[]
    for x in lst:
        lst2.append(lst[i].upper())
        i+=1
    return lst2

lst = ["fs", "trt", "fdscv"]
capitalize_items_2(lst)
print lst2

Try:
Code:
def capitalize_items_2(lst):
    i=0
    lst2=[]
    for x in lst:
        lst2.append(lst[i].upper())
        i+=1
    return lst2

lst = ["fs", "trt", "fdscv"]
lst2 = capitalize_items_2(lst)
print(lst2) # I'm using print as a function for forwards compatibility with Python 3
 
Ooh I see now.. so I actually need to assign the function to something
 
Okay it works now thanks
 
Also, FYI, list types have a method called index, which will return the index of the item. So, you could just write:
Code:
def capitalizeItems3(lst):
    lst2 = []
    for item in lst:
        lst2.append(lst[lst.index(item)].upper())
    return lst2

Glad to help.
 
  • #10
jhae2.718 said:
Also, FYI, list types have a method called index, which will return the index of the item. So, you could just write:
Code:
def capitalizeItems3(lst):
    for item in lst:
        lst[lst.index(item)] = lst[lst.index(item)].upper()
    return lst

Glad to help.

Oh that's good haha.
Seems like I always code things inefficiently :/
 
  • #11
My example actually would replace the original list; I've fixed it above. Sorry, it's late here.

zeion said:
Oh that's good haha.
Seems like I always code things inefficiently :/

Practice. No one ever wrote great code when they first started learning. :smile:
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
55
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
7K
Replies
9
Views
3K
Replies
35
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K