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

In summary, the conversation discusses writing a function to capitalize items in a list and the use of local variables within functions. The solution provided involves defining a separate list and using a for loop to iterate through the original list and append the capitalized items to the new list. The issue of returning a list defined within a function is also addressed and alternative methods, such as list comprehensions and the map function, are suggested. The conversation concludes with a reminder to practice coding in order to improve efficiency.
  • #1
zeion
466
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
  • #2
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.
 
  • #3
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.)
 
  • #4
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?
 
  • #5
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.
 
  • #6
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
 
  • #7
Ooh I see now.. so I actually need to assign the function to something
 
  • #8
Okay it works now thanks
 
  • #9
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:

What is Python?

Python is a high-level, interpreted programming language that was created by Guido van Rossum in 1991. It is known for its simple syntax and readability, making it a popular choice for beginners.

What can I use Python for?

Python can be used for a variety of applications, including web development, data analysis, artificial intelligence, scientific computing, and more. It is a versatile language that is used in many industries.

How do I install Python?

To install Python, you can visit the official Python website and download the latest version for your operating system. There are also many online resources and tutorials available to help guide you through the installation process.

What are the basic data types in Python?

The basic data types in Python include integers, floats, strings, booleans, and lists. Integers are whole numbers, floats are numbers with decimals, strings are sequences of characters, booleans represent true or false values, and lists are collections of items.

What is the difference between Python 2 and Python 3?

Python 2 and Python 3 are different versions of the Python programming language. Python 2 was released in 2000 and Python 3 was released in 2008. Python 3 is the newer version and has some syntax changes and improvements over Python 2, but both versions are still widely used.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
3
Views
311
Replies
9
Views
1K
  • Programming and Computer Science
Replies
31
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
4
Views
911
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top