On 14/08/18 22:38, Cameron Simpson wrote:
> If you're trying to partition words into values starting with "x" and values
> not starting with "x", you're better off making a separate collection for the
> "not starting with x" values. And that has me wondering what the list "b" in
> your code
On 14/08/18 23:16, Peter Otten wrote:
> For a simple solution you do need a and b: leave words unchanged, append
> words starting with "x" to a and words not starting with "x" to b.
>
> Someone familiar with Python might do it with a sort key instead:
Or, for one definition of simple, a list
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:
Thanks all. This is very helpful. I am new to Python :)
Sent from my iPhone
> On 15 Aug 2018, at 8:16 am, Peter Otten <__pete...@web.de> wrote:
>
> Alan Gauld via Tutor wrote:
>
>>> On 14/08/18 09:11, Deepti K wrote:
>>> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
>>>
Alan Gauld via Tutor wrote:
> On 14/08/18 09:11, Deepti K wrote:
>> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
>> function, it picks up only 'xzz' and not 'xaa'
>
> Correct because
>
>> def front_x(words):
>> # +++your code here+++
>> a = []
>> b = []
>>
On 14Aug2018 18:11, Deepti K 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)
On 14/08/18 09:11, Deepti K wrote:
> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
> function, it picks up only 'xzz' and not 'xaa'
Correct because
> def front_x(words):
> # +++your code here+++
> a = []
> b = []
> for z in words:
> if z.startswith('x'):
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