Re: [Tutor] Query: lists

2018-08-14 Thread Alan Gauld via Tutor
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

Re: [Tutor] Query: lists

2018-08-14 Thread Alan Gauld via Tutor
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

Re: [Tutor] Query: lists

2018-08-14 Thread Nitin Madhok
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:

Re: [Tutor] Query: lists

2018-08-14 Thread Deepti K
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 >>>

Re: [Tutor] Query: lists

2018-08-14 Thread Peter Otten
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 = [] >>

Re: [Tutor] Query: lists

2018-08-14 Thread Cameron Simpson
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)

Re: [Tutor] Query: lists

2018-08-14 Thread Alan Gauld via Tutor
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'):

[Tutor] Query: lists

2018-08-14 Thread Deepti K
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

Re: [Tutor] Is there a better way to write my code?

2018-08-14 Thread Neil Cerutti
On 2018-08-14, Alan Gauld via Tutor wrote: > - Use a list for objects(*) where you might need to > change the value of one of the objects A list is standard practice when you need an ordered collection of objects of the same type, e.g., a bunch of numbers, a bunch of chickens, etc.. It is most

Re: [Tutor] Is there a better way to write my code?

2018-08-14 Thread Alan Gauld via Tutor
On 14/08/18 07:56, Rafael Knuth wrote: > List comprehension is really cool. One thing I like about list > comprehension is that you can get a dictionary, tuples or lists as a > result by just changing the type of braces. > > # dictionary > colors = ["red", "blue", "white", "yellow"] > colors_len

Re: [Tutor] Is there a better way to write my code?

2018-08-14 Thread Nitin Madhok
Use List comprehension: animals = ["Dog", "Tiger", "SuperLion", "Cow", "Panda"] animals_lol = [[animal, len(animal)] for animal in animals] print(animals_lol) [['Dog', 3], ['Tiger', 5], ['SuperLion', 9], ['Cow', 3], ['Panda', 5]] If you want to read more about list comprehension,

Re: [Tutor] Is there a better way to write my code?

2018-08-14 Thread Rafael Knuth
> I wrote this code below > I was wondering if there is a shorter, more elegant way to accomplish this > task. > Thanks! thank you so much everyone! List comprehension is really cool. One thing I like about list comprehension is that you can get a dictionary, tuples or lists as a result by just

Re: [Tutor] Syntax question

2018-08-14 Thread Matthew Polack
Just a quick 'Thank you' for this advice the other day Alan and Abdur-Rahmaan. Greatly appreciated as we work together with our students here. Thank you. Matthew Polack | Teacher [image: Emailbanner3.png] Trinity Drive | PO Box 822 Horsham Victoria 3402 p. 03 5382 2529 m. 0402456854