Python Forum
Capitalize first letter of names in nested loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Capitalize first letter of names in nested loops
#1
Hi guys,

I got this:

names=[['peter','jack','bo'],'eva',['christian','joanne',7],'5']
#Make all names in nested loop above with capital first letters, dont make new list but change current list
#elements 7 and '5' should be left as is

I can't seem to figure out how to extract these names and make it the words capital letters.

I currently am thinking of looping through the names and trying to use names.capitalize()
but can't do that on lists and all the names on the list are not capitalized as the ones in the nested lists are untouched.

Any ideas on what to do with this silly challenge?
Reply
#2
what have you tried so far?
show code
Reply
#3
for list in names:
    for name in list:
        name.title()
        print(name)
I am really lost as to how I can loop through all the lists and change it.
Reply
#4
(Oct-25-2019, 10:09 AM)Larz60+ Wrote: what have you tried so far?
show code

Wrote it down in replies, sorry
Reply
#5
change line 3 from
name.title()
To
name = name.title()
Reply
#6
(Oct-25-2019, 03:11 PM)Larz60+ Wrote: change line 3 from
name.title()
To
name = name.title()

names=[['peter','jack','bo'],'eva',['christian','joanne',7],'5']
#Make all names in nested loop above with capital first letters, dont make new list but change current list
#elements 7 and '5' should be left as is

for list in names:
    for name in list:
        name = name.title()
        print(name)

print(names)
that does seem to make all but "eva" capital letters and prints: AttributeError: 'int' object has no attribute 'title'

which is probably as the list includes 7

furthermore, I cant print the list afterward. I don't think this for loop actually changes values within the names list?
Reply
#7
first, you cannot name a variable 'list' as it is a reserved for a python sequence.
next you need to properly index nested lists
then check type for string
and capitalize if so
Reply
#8
Seems like a job for recursion. What I would do is define a function that tests the type of the argument. If the argument is a list, it calls itself repeatedly on the items in the list. If the type is a string it capitalizes it and prints. If it is an int it just prints. Call it on your source list and it works.
Reply
#9
(Oct-27-2019, 01:00 AM)jefsummers Wrote: Seems like a job for recursion.

That was my idea as well. However, if you need to change the list in place, note that you need to assign the capitalized string back to the list itself (specifically the index of the uncapitalized string). So it's a loop that recurses on lists, does item assignment to capitalize strings, and ignores everything else.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
I also thought about recursion, but since a homework assignment, thought perhaps not allowed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with nested loops robert5502001 7 3,602 Aug-01-2022, 06:26 AM
Last Post: stevensanders
  computing average in nested loops cap510 5 5,186 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  nested for loops to recursion ashkea26 4 3,505 Nov-02-2018, 05:00 PM
Last Post: ichabod801
  Nested loops in openCV JimmyJangle 1 4,831 Apr-17-2018, 04:10 AM
Last Post: Mekire
  capitalize all letters atux_null 5 3,881 Dec-03-2017, 07:07 PM
Last Post: buran
  Nested for loops with numbers Liquid_Ocelot 7 5,869 Aug-15-2017, 06:38 AM
Last Post: nilamo
  Nested loops, lists and if statements Liquid_Ocelot 10 8,987 Apr-23-2017, 02:02 PM
Last Post: Mekire
  Captalizing the letter from the first two names riko 6 6,303 Mar-10-2017, 10:11 PM
Last Post: riko

Forum Jump:

User Panel Messages

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