简体   繁体   中英

How to get only lowercase strings from a list using list comprehension

The question asked:

Use list comprehensions to generate a list with only the lowercase letters in my_list. Print the result list.

['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D']

My code:

my_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D']
hi = ([ char for char in range(len(my_list)) if char%2 == 0])

print(hi)

I tried it out, but got integers as answers and not the strings I wanted.

Note: several answers here assume that what you want is to select the values in the list that are lowercase. This answer assumes that that was an example and that the thing you're trying to do is to select the values in the list that occur at every other list index. (This seems to me to be the correct interpretation, because that's what the implementation in the question appears to be trying to do.) I'm not sure who misunderstood the question here, but since the question can be interpreted multiple ways, I think the question is probably at fault here. Until the question is clarified, I think it should be placed on hold.

The simplest and fastest way to do this is with a slice:

print(my_list[::2])  # Slice the whole list, with step=2

To replicate the logic you're describing, where you want to take the values with indexes that are modulo 2, then you need to generate both the indexes and the values for your list in the comprehension, and use one for the filtering and the other for the result:

hi = [ch for ix, ch in enumerate(my_list) if ix % 2 == 0]

Python strings have islower method. Also, you can directly iterate over the list, no need to check its length or the parity of the indexes.

my_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D']
hi = [char for char in my_list if char.islower()]
print(hi)
# ['a', 'b', 'c', d']

Your list comprehension:

[char for char in range(len(my_list)) if char%2 == 0]

Will produce integers instead of characters. This is because range(len(my_list)) gives you indices. You instead need to get the characters.

This can be done using enumerate() :

 [char for i, char in enumerate(my_list) if i % 2 == 0]

Or a less pythonic approach, using just indexing my_list :

[my_list[i] for i in range(len(my_list)) if i % 2 == 0]

You can also just filter out the lowercase letters with str.islower() :

[char for char in my_list if char.islower()]

Which avoids having to use indices altogether.

You can use list comprehension as following where you iterate over your individual elements and check if it is a lower case using .islower()

my_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D']

lower = [i for i in my_list if i.islower()]
# ['a', 'b', 'c', 'd']
my_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D']
res = [ char for char in  my_list if ord(char)>=97]

using islower() function

l = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D']
result = [el for el in l if el.islower()]

To add a range(len(my_list)) that create the following range(0, 8) and char, in this case, is an integer and you create a list of integers.

To generate a list with only the lowercase letters use 'islower' method hi = ([ char for char in my_list if char.islower()])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM