0

I need to know if the letter b (both uppercase and lowercase preferably) consist in a list.

My code:

List1=['apple', 'banana', 'Baboon', 'Charlie']
if 'b' in List1 or 'B' in List1:
    count_low = List1.count('b')
    count_high = List1.count('B')
    count_total = count_low + count_high   
    print "The letter b appears:", count_total, "times."
else:
    print "it did not work"

4 Answers 4

1

You need to loop through your list and go through every item, like so:

mycount = 0
for item in List1:
    mycount = mycount + item.lower().count('b')

if mycount == 0:
    print "It did not work"
else:
    print "The letter b appears:", mycount, "times"

Your code doesn't work since you're trying to count 'b' in the list instead of each string.

Or as a list comprehension:

mycount = sum(item.lower().count('b') for item in List1)
Sign up to request clarification or add additional context in comments.

6 Comments

To account for both upper/lowercase, I'd change the mycount statement to use item.lower().count('b')
Thanks. Is there a way though to search in the list without searching in each string?
mycount = "".join(List1).lower().count('b')
@Robo What sal said above^. My solution is just a nice and easy solution to understand :)
I just wanted to add that your solution also works for searching for characters in lists that are in other lists like tuples. That was what I really need so thanks again!
|
0

So the question is why doesn't this work?

Your list contains one big string, "apple, banana, Baboon, Charlie".

Add in the single quotes between elements.

1 Comment

I edited so that the lists now contains multiple keys. Beginners mistake :). It still doesn't work though.
0

Based on your latest comment, the code could be rewritten as:

count_total="".join(List1).lower().count('b')
if count_total:
   print "The letter b appears:", count_total, "times."
else:
   print "it did not work"

You basically join all the strings in the list and make a single long string, then lowercase it (since you don't care about case) and search for lowercase(b). The test on count_total works, because it evals to True if not zero.

1 Comment

Works well and easy to understand. Thanks for this.
0

The generator expression (element.lower().count('b') for element in List1) produces the length for each element. Pass it to sum() to add them up.

List1 = ['apple', 'banana', 'Baboon', 'Charlie']
num_times = sum(element.lower().count('b')
                for element in List1)
time_plural = "time" if num_times == 1 else "times"
print("b occurs %d %s"
      % (num_times, time_plural))

Output:

b occurs 3 times

If you want the count for each individual element in the list, use a list comprehension instead. You can then print this list or pass it to sum().

List1 = ['apple', 'banana', 'Baboon', 'Charlie']
num_times = [element.lower().count('b')
             for element in List1]
print("Occurrences of b:")
print(num_times)
print("Total: %s" % sum(b))

Output:

Occurrences of b:
[0, 1, 2, 0]
Total: 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.