This question already has answers here:
What does enumerate() mean?
(7 answers)
Closed 1 year ago.
def capital_indexes(string):
indexes = []
for i, s in enumerate(string):
if s.isupper():
indexes.append(1)
return indexes
What does the s in the for loop mean?
i is the index in the string, while s is a string of length 1 representing the letter at that index.
See documentation for enumerate
Related
This question already has answers here:
How to remove even numbers from a list in Python? [duplicate]
(5 answers)
How to remove items from a list while iterating?
(25 answers)
Python: remove odd number from a list
(9 answers)
Strange result when removing item from a list while iterating over it
(8 answers)
Closed last year.
I am having some trouble while removing even integers from a list in Python. This is what I am trying to do but I can't figure out what am I doing wrong. Is the array skipping elements due to items being removed? I would really appreciate some help.
def removeEven(l):
for e in l:
if e % 2 == 0:
l.remove(e)
print(l)
Try filter:
def removeEvent(l):
return list(filter(lambda x: x % 2 == 0, l))
print(removeEvent(l))
Or even better:
print(list(filter(lambda x: x % 2 == 0, l)))
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 3 years ago.
Given this code:
nums = [1,2,3]
def test1(nums):
for i in nums:
if i == 1:
return True
return False
test1(nums)
The result is True even though after the for loop there is a return False. What am I missing?
This question already has an answer here:
Identifying straight, flush and other categories (from Poker) using Python
(1 answer)
Closed 3 years ago.
If there is a string = '99888' it should print 'True'. How can you check pairs and threes of a character in a string
I tried using the count function but it only identifies a pair
string = '99888'
for c in '123456789':
if string.count(c) == 2 and string.count(c) == 3 :
print('True')
Edit:
The string is a always 5 character string and if there is a pair and three of a kind it prints True
For example '89899' and '75757' print True. '98726' prints False
Use the Counter class from the collections module
from collections import Counter
def check(inputString):
x = Counter(inputString)
list_of_counts = [x[i] for i in x.elements()]
if (2 in list_of_counts) and (3 in list_of_counts):
return(True)
else:
return(False)
print(check("99888"))
print(check("999888"))
This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
How do I add together integers in a list in python?
(4 answers)
Closed 3 years ago.
My Objective: To find and print the sum of all the items in the list via function
My Code:
def list_sum(x):
if type(x)!='list':
print("Invalid List item!")
if type(x)=='list':
list_length = len(x)
total = 0
i = 0
for i in range (list_length):
total +=x[i]
i+=1
print("The sum of all items in the list is: ",total)
samples = [1,3,5,6,8,45,67,89]
list_sum(samples)
My Output:
<class 'list'>
Invalid List item!
Expected:
224
Why am I getting the output I am getting?
if type(x) != list
not
if type(x) != 'list'
This question already has answers here:
Concatenating two lists - difference between '+=' and extend()
(11 answers)
Python append() vs. + operator on lists, why do these give different results?
(7 answers)
Closed 6 years ago.
Python lists have a += operator as well as append and extend methods.
If l is a list, is l += ... equivalent to l.append(...), l.extend(...), both, or neither?
In python += on a list is equivalent to extend method on that list.