User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,534 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1123 | Replies: 11
Reply
Join Date: Oct 2007
Posts: 6
Reputation: MrShoot is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MrShoot MrShoot is offline Offline
Newbie Poster

Working with lists

  #1  
Oct 6th, 2007
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:

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

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

  1. for char in list:
  2. 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 27
Reputation: StrikerX11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
StrikerX11 StrikerX11 is offline Offline
Light Poster

Re: Working with lists

  #2  
Oct 7th, 2007
>>> 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']
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: MrShoot is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MrShoot MrShoot is offline Offline
Newbie Poster

Re: Working with lists

  #3  
Oct 7th, 2007
Interesting, could you explain me a bit this part?

  1. conv_list +=[el.lower()]

What exactly would the += do there?
Reply With Quote  
Join Date: Sep 2007
Posts: 19
Reputation: paddy3118 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
paddy3118 paddy3118 is offline Offline
Newbie Poster

Tutorial Re: Working with lists

  #4  
Oct 7th, 2007
Originally Posted by StrikerX11 View Post
>>> 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.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Working with lists

  #5  
Oct 7th, 2007
Originally Posted by MrShoot View Post
Interesting, could you explain me a bit this part?

  1. conv_list +=[el.lower()]

What exactly would the += do there?
This is just a shorthand of ...
  1. my_list = ["CANADA", "HELLO", "I TOLD YOU SO"]
  2. conv_list=[]
  3. for el in my_list:
  4. conv_list = conv_list + [el.lower()]
  5. print conv_list
  6. """
  7. my output -->
  8. ['canada', 'hello', 'i told you so']
  9. """
I think it would be more pythonic to use the list method append() ...
  1. my_list = ["CANADA", "HELLO", "I TOLD YOU SO"]
  2. conv_list=[]
  3. for el in my_list:
  4. conv_list.append(el.lower())
  5. print conv_list
  6. """
  7. my output -->
  8. ['canada', 'hello', 'i told you so']
  9. """
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: MrShoot is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MrShoot MrShoot is offline Offline
Newbie Poster

Re: Working with lists

  #6  
Oct 7th, 2007
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:

  1. 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.
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: boni_go is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boni_go boni_go is offline Offline
Newbie Poster

Re: Working with lists

  #7  
Oct 7th, 2007
Hi
you can check the first letter:
[code = python]
for el in my_list:
conv_list.append(el[0] + el[1:].lower())
[/code]

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
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: MrShoot is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MrShoot MrShoot is offline Offline
Newbie Poster

Re: Working with lists

  #8  
Oct 7th, 2007
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!
Reply With Quote  
Join Date: Sep 2007
Posts: 19
Reputation: paddy3118 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
paddy3118 paddy3118 is offline Offline
Newbie Poster

Tutorial Re: Working with lists

  #9  
Oct 8th, 2007
This should help:
>>> s = 'CAnADA'
>>> s.capitalize()
'Canada'
>>> 

- Paddy.
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Working with lists

  #10  
Oct 8th, 2007
depending on what you want ...

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

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

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

Jeff
Last edited by jrcagle : Oct 8th, 2007 at 11:12 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 4:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC