Reputation: 17
If the character at index 0 of the current item is the letter "a", continue to the next one. Otherwise, print out the current member.
Example: ["abc", "xyz"]
will just print "xyz".
def loopy(items):
for item in items:
if item[0] == "a":
continue
else:
print(items)
Upvotes: 0
Views: 2007
Reputation: 2788
with few correction :
>>> l=['abi', 'crei', 'fci', 'anfe']
>>> def loopy(list):
... for i in list:
... if i[0]=='a':
... continue
... else:
... print i
...
>>> loopy(l)
crei
fci
that you can make shorter this way :
>>> def loopy(list):
... for i in list:
... if i[0]!='a':
... print i
...
>>> loopy(l)
crei
fci
or to print in one line :
>>> def loopy3(list):
... for i in list:
... print i if i[0]!='a' else '',
...
>>> loopy3(l)
crei fci
but you can also use a list comprehension as suggest by bogdanciobanu :
>>> def loopy2(list):
... print [i for i in list if i[0]!='a']
>>> loopy2(l)
['crei', 'fci']
Upvotes: 0
Reputation: 403040
How about a filter
?
In [191]: print('\n'.join(filter(lambda x: x[0] != 'a', ["abc", "xyz", "test"])))
xyz
test
Upvotes: 1
Reputation: 8388
def loopy(items):
for item in items:
if not item.startswith('a'):
print(item)
Upvotes: 0