Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List capital letters
#1
Hello again! Smile

I'm trying to return a list of all the indexes in the string that have capital letters.

lista =[]
def capital_letters(arg):
   for i in arg:
       if i.isupper():
                            #place the capital letters in the list
           return lista

print(capital_letters('tZtTtT'))
I tried:

lista.append(i)
but it return only the first capital letter in the string.
Reply
#2
The return is indented inside of the if statement so it will return straight away.
The return needs to be indented level with the for statement, it then will loop through all the letters before it returns the list.
It would also be a good idea to move the definition lista = [] into the start of the function.
Reply
#3
This should work.

lista =[]
def capital_letters(arg):
   for i in arg:
       if i.isupper():
           lista.append(i) # append the i to the list
   return lista # return the list after the for-loop is done
 
print(capital_letters('tZtTtT'))
arg, i and lista is a bad name.

You could write the function as generator:
def filter_capital_letters(text):
    for char in text:
        if char.isupper():
            yield char
            # the yield statement makes a generator
            # the generator must be consumed

list(filter_capital_letters('tZtTtT'))
Or you could write it as list comprehension or generator expression.
text = 'Hello World'
upper_chars = [char for char in text if char.isupper()]

upper_chars_genetator = (char for char in text if char.isupper())
# upper_chars_generator does nothing until it's consumed by
# a list/tuple or in a for-loop
# this interesting for data, which does not fit in memory.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
You also aren't doing anything to get the index of each character in the string (see the enumerate function for that).

This can be quite concisely written using a list/set comprehension or a generator expression, too (I'll show that once you have a working version).
Reply
#5
In [2]: list(enumerate('My Name is andre'))                                                 
Out[2]: 
[(0, 'M'),
 (1, 'y'),
 (2, ' '),
 (3, 'N'),
 (4, 'a'),
 (5, 'm'),
 (6, 'e'),
 (7, ' '),
 (8, 'i'),
 (9, 's'),
 (10, ' '),
 (11, 'a'),
 (12, 'n'),
 (13, 'd'),
 (14, 'r'),
 (15, 'e')]
In a for-loop:

text = 'My name is Andre.'
for index, char in enumerate('My name is Andre.'):
    print(index, char)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Thank you DeaD_EyE! Thank everyone!
This is such a smarter way.

text = 'Hello World'
upper_chars = [char for char in text if char.isupper()]
think in terms of putting (everytime) a code inside a function is not a god pratice. I think. In Python we have so many possibilities. So many ways to do the same thing.
Reply
#7
For completeness, the set comprehension is

text = ...
{i for (i, c) in enumerate(text) if c.isupper()}
Remember that sets are O(1) lookup and don't store their elements in a particular order.
Reply
#8
text = ...
{i:c for (i, c) in enumerate(text) if c.isupper()}
Dict-Comprehension. The only change is the colon. In this case the keys are the indices and the values are the chars.
By the way, ... is the Ellipsis object. It's used for numpy.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Thank you guys!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,945 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Pynput - no capital letters jmair 2 4,544 Feb-12-2019, 09:53 PM
Last Post: jmair
  make a list of string in capital by using a function anancba 3 3,431 Nov-23-2017, 06:42 AM
Last Post: heiner55
  Ending loop with string (capital & lowercase) MattWilk97 3 3,441 Oct-22-2017, 09:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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