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
AI Thread Summary
The discussion centers around creating a Python function, `capitalize_items_2`, which takes a list of strings and returns a new list with all items capitalized. The initial implementation uses a loop to append uppercase versions of the strings to a new list, but there are questions about variable scope and the need to define the new list outside the function. It is clarified that local variables, like `lst2`, exist only within the function's scope and cannot be accessed outside of it. Users are advised to store the function's return value in a variable to use it, and suggestions are made to optimize the code using list comprehensions or the `map` function. The importance of practicing coding to improve efficiency and understanding is also emphasized.
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:
Back
Top