•
•
•
•
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,533 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,886 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
![]() |
•
•
•
•
(...)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:
I want it to show up like:python Syntax (Toggle Plain Text)
list = ["CAnADA", "hELLO", "CAN"]
["Canada", "hello", "Can"]
I recommend avoiding using variable names like list as it can get very confusing. Anyhow, a list comprehension could help here:
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
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()) """
drink her pretty
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Help with inline lists for horizontal navigation (HTML and CSS)
- Linked Lists C++ Sorting Integers (C++)
- help...one-dimensional array averaging program (C++)
- function for merging two lists (C++)
- Looking up and displaying values in linked lists (C++)
- Compare 2 Lists of Words (MySQL)
- bdk1.1 and j2sdk (Java)
Other Threads in the Python Forum
- Previous Thread: initalze class by a condition
- Next Thread: What's your favorite error?



Linear Mode