Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list manipulation
#4
First, note that there is a capitalize method of strings:

>>> 'spam'.capitalize()
'Spam'
So you don't need to break the word apart, uppercase the first letter, and put it back together.

Second, when you loop through the list of words at the end, items is a copy of the word, not the word in the list. So you either need to get at the word in the list, or build a new list. Enumerate could get you at the word in the list:

numbers = [8, 0, 1]
for num_index, num in enumerate(numbers):
    if num % 2:
        numbers[num_index] = num * 3 + 1
    else:
        numbers[num_index] = num // 2
print(numbers)
Also note that 'in' can be used as an operator to check if something is in a list or other container. You can just use if items in exceptions:.

In terms of efficiency, you are going through the list twice: once to upper case, then once to lower case. I would go through the list once, starting with a second empty list. Check each word as you go as to whether it needs to be capitalized or not, and then append the word with the appropriate capitalization to the second list. Also, the in operator works faster with sets, so you might want to make exceptions a set.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
list manipulation - by cameronwood611 - Oct-03-2017, 03:40 AM
RE: [HELP] list manipulation - by ichabod801 - Oct-03-2017, 03:44 AM
RE: [HELP] list manipulation - by cameronwood611 - Oct-03-2017, 02:26 PM
RE: list manipulation - by ichabod801 - Oct-03-2017, 02:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in list manipulation CyKlop 6 2,356 Oct-18-2021, 09:03 AM
Last Post: DeaD_EyE
  How to pass a list by value for manipulation within a process? bRitch022 4 2,749 Jul-09-2020, 07:13 PM
Last Post: bRitch022
  list or dictionary manipulation dtigue 5 105,373 Jul-21-2017, 03:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020