0

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)
6
  • 7
    print(items) -> print(item) Commented Jul 20, 2017 at 20:49
  • you want all items not starting with a or all items after the first starting with a? Commented Jul 20, 2017 at 20:50
  • 1
    Agree with @MosesKoledoye but you should post what the specific error is (which you did not do) and what is expected (which you did). Commented Jul 20, 2017 at 20:51
  • and also you have an indentation problem Commented Jul 20, 2017 at 20:51
  • @bogdanciobanu He wants all items that DO NOT start with 'a' Commented Jul 20, 2017 at 20:52

3 Answers 3

1

How about a filter?

In [191]: print('\n'.join(filter(lambda x: x[0] != 'a', ["abc", "xyz", "test"])))
xyz
test
Sign up to request clarification or add additional context in comments.

2 Comments

Does he want a list or he simply wants to print items (i.e., one per line)?
@AGNGazer The question is open to interpretation :) Probably the latter, but that's just an extension of this.
0
def loopy(items):
    for item in items:
        if not item.startswith('a'):
            print(item)

Comments

0

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']

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.