Deepti,
What you’re seeing happens because you are making changes (words.remove(z)) to
the list while you are iterating over it (for z in words). If your goal is to
print the original words, removed words and original words without removed
words, you could do something like this using sets:
words = ['bbb', 'ccc', 'axx', 'xzz', 'xaa']
def front_x(words):
# +++your code here+++
removed_words = []
for word in words:
if word.startswith('x'):
removed_words.append(word)
print 'word is', word
print 'original', sorted(words)
print 'new', sorted(removed_words)
print sorted(list(set(removed_words + words)))
If you also wanted to get the original words after removing the removed_words,
you could print them like this:
print 'original - new', list(set(words) - set(removed_words))
Or you could even use list comprehension like this:
print 'original - new', [word for word in words if word not in removed_words]
--
Thanks,
Nitin Madhok
Clemson University
CONFIDENTIALITY NOTICE
This e-mail, and any attachments thereto, is intended only for use by the
addressee(s) named herein and may contain privileged and/or confidential
information. If you are not the intended recipient of this e-mail, any
dissemination, distribution or copying of this e-mail, and any attachments
thereto, is strictly prohibited. If you have received this e-mail in error,
please immediately notify the sender by e-mail or telephone and permanently
delete all copies of this e-mail and any attachments.
> On Aug 14, 2018, at 4:11 AM, Deepti K <[email protected]> wrote:
>
> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
> function, it picks up only 'xzz' and not 'xaa'
>
> def front_x(words):
> # +++your code here+++
> a = []
> b = []
> for z in words:
> if z.startswith('x'):
> words.remove(z)
> b.append(z)
> print 'z is', z
> print 'original', sorted(words)
> print 'new', sorted(b)
> print sorted(b) + sorted(words)
>
> Thanks,
> Deepti
> _______________________________________________
> Tutor maillist - [email protected]
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor