Hi, this is my first forum message so I'll make a brief introduction. I'm from Canada and I am Computer Science student. I enjoy working with computers, specially web programming. It has come to my attention (and also a school requirement) to learn Python. So here I am with my first question:

Suppose I have the following list:

list = ["CANADA", "HELLO", "I TOLD YOU SO"]

And I want to make each element from list lowercase. This is what I have so far:

for char in list:
     char.lower()

That will lowercase each of my elements. Up till that part everything is fine. However, let's say I want to return the string again, but this time. With all its elements properly lowercased. And this is where I'm stuck. I can't think of a way to do this.

Any help / ideas / suggestions will be greatly appreciated.

>>> LIST = ["CANADA", "HELLO", "I TOLD YOU SO"]
>>> conv_list=[]
>>> for el in LIST:
    conv_list +=[el.lower()]

    
>>> conv_list
['canada', 'hello', 'i told you so']

Interesting, could you explain me a bit this part?

conv_list +=[el.lower()]

What exactly would the += do there?

>>> LIST = ["CANADA", "HELLO", "I TOLD YOU SO"]
>>> conv_list=[]
>>> for el in LIST:
    conv_list +=[el.lower()]

    
>>> conv_list
['canada', 'hello', 'i told you so']

You might also try:

list_in = ["CANADA", "HELLO", "I TOLD YOU SO"]
list_out = []
for element in list_in:
  list_out.append(element.lower()]

You should find it easier to find out what the append method does using your documentation.

Python has a nifty feature called list comprehensions that would also make a good solution, but if you haven't come across them yet don't worry:

list_in = ["CANADA", "HELLO", "I TOLD YOU SO"]
list_out = [element.lower()  for element in list_in]

- Paddy.

Interesting, could you explain me a bit this part?

conv_list +=[el.lower()]

What exactly would the += do there?

This is just a shorthand of ...

my_list = ["CANADA", "HELLO", "I TOLD YOU SO"]
conv_list=[]
for el in my_list:
    conv_list = conv_list + [el.lower()]
print conv_list
"""
my output -->
['canada', 'hello', 'i told you so']
"""

I think it would be more pythonic to use the list method append() ...

my_list = ["CANADA", "HELLO", "I TOLD YOU SO"]
conv_list=[]
for el in my_list:
    conv_list.append(el.lower())
print conv_list
"""
my output -->
['canada', 'hello', 'i told you so']
"""

Perfect, yes I was aware of the append method. That's the way I did it after Striker put his solution up. I just didn't see it all the way through. Thanks a lot folks.

Now let me complicate this a bit more. What if I only want to lowercase all the letters but the first letter from each string included in the list? For example, we have:

list = ["CAnADA", "hELLO", "CAN"]

I want it to show up like:

["Canada", "hello", "Can"]

I was thinking in using an if-else statement but I don't know whether to check the first letter for each string on a list is capital or not.

Hi
you can check the first letter:

for el in my_list:
   conv_list.append(el[0] + el[1:].lower())

el[1:] = the string without the first char
for example:
el = "CAnADA"
el[0] = "C"
el[1:] = "AnADA"
el[1:].lower() = "anada"
el[0] + el[1:].lower() = "Canada" - as you want

Wow, what a beauty. I guess I got confused about adding data from each element to the list. You sir are a genious, thanks a lot!

This should help:

>>> s = 'CAnADA'
>>> s.capitalize()
'Canada'
>>>

- Paddy.

depending on what you want ...

>>> s = "now is the time for ALL good men..."
>>> s.capitalize()
'Now is the time for all good men...'
>>> s.title()
'Now Is The Time For All Good Men...'
>>>

strings have number of powerful methods; you might poke around with

dir(str)
and
help(str.M), where M is some method name.

Jeff

(...)Now let me complicate this a bit more. What if I only want to lowercase all the letters but the first letter from each string included in the list? For example, we have:

list = ["CAnADA", "hELLO", "CAN"]

I want it to show up like:
["Canada", "hello", "Can"]

I recommend avoiding using variable names like list as it can get very confusing. Anyhow, a list comprehension could help here:

mylist  = ["CAnADA", "hELLO", "CAN"]
newlist = [ x[0]+x[1:].lower() for x in mylist ]
print newlist

['Canada', 'hello', 'Can'] Things get a bit hairer if you allow empty strings in mylist, but that's an exercise for the reader.

If you have empty strings in the list you can change BearofNH's code simply:

mylist  = ["CAnADA", "hELLO", "", "CAN"]
# use list comprehension
newlist = [ x[0]+x[1:].lower() for x in mylist if x ]
print newlist
"""
my output -->
['Canada', 'hello', 'Can']

a list comprehension is a more efficient way of coding this ...
newlist =[]
for x in mylist:
    if x != "":
        newlist.append(x[0]+x[1:].lower())
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.