Re: [Tutor] How to print certain elements
On 21/01/2014 10:24, Mkhanyisi Madlavana wrote: How would I print washington and monroe using [:]? print X[::3] How would I print every element but those two names? print X[1::2] On 21 January 2014 12:18, Alan Gauld mailto:alan.ga...@btinternet.com>> wrote: On 21/01/14 06:18, Adriansanchez wrote: Hello everyone, I am newbie to Python and programming in general. My question is, given a list: X=['washington','adams','__jefferson','madison','monroe'] And a string: Y='washington,adams,jefferson,__madison,monroe' How would I print washington and monroe using [:]? How would I print every element but those two names? The [:] syntax is used for selecting a range of values from a starting point to a finish. Its not appropriate for selecting arbitrary items out of the list. If you know which items you want you can use a simple index to access them (remember the first item is index 0) So to print the first item and the fourth item: print(X[0],X[3]) In your case it's the first and last so we can do a similar thing: print(X[0], X[4]) But for the last element we can alternatively use a shortcut to save counting the indexes; that's use an index of -1: print(X[0],X[-1]) Printing every element except those two is harder. The simplest approach is to use a loop to process the list and test each value: for name in X: if name not in (X[0], X[-1]): print name For the special case of excluding the first and last names you could use the [:] notation like this: print X[1:-1] But that only works where you want *all* the names in a sequence between two end points. Finally there is a more advanced way of filtering out items from a list called a list comprehension: print ( [name for name in X if name not in (X[0],X[-1])] ) Which is pretty much our 'for' loop above, written in a shorthand single line form. hth If you must top post please get your facts right. In [1]: X=['washington','adams','jefferson','madison','monroe'] In [2]: print(X[::3]) ['washington', 'madison'] In [3]: print(X[1::2]) ['adams', 'madison'] -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print certain elements
How would I print washington and monroe using [:]? print X[::3] How would I print every element but those two names? print X[1::2] On 21 January 2014 12:18, Alan Gauld wrote: > On 21/01/14 06:18, Adriansanchez wrote: > >> Hello everyone, >> I am newbie to Python and programming in general. My question is, given a >> list: >> X=['washington','adams','jefferson','madison','monroe'] >> And a string: >> Y='washington,adams,jefferson,madison,monroe' >> >> How would I print washington and monroe using [:]? >> How would I print every element but those two names? >> > > The [:] syntax is used for selecting a range of values > from a starting point to a finish. Its not appropriate > for selecting arbitrary items out of the list. > > If you know which items you want you can use a simple > index to access them (remember the first item is index 0) > > So to print the first item and the fourth item: > > print(X[0],X[3]) > > In your case it's the first and last so we can do > a similar thing: > > print(X[0], X[4]) > > But for the last element we can alternatively use > a shortcut to save counting the indexes; that's use > an index of -1: > > print(X[0],X[-1]) > > Printing every element except those two is harder. > The simplest approach is to use a loop to process > the list and test each value: > > for name in X: > if name not in (X[0], X[-1]): >print name > > For the special case of excluding the first and > last names you could use the [:] notation like > this: > > print X[1:-1] > > But that only works where you want *all* the > names in a sequence between two end points. > > Finally there is a more advanced way of filtering > out items from a list called a list comprehension: > > print ( [name for name in X if name not in (X[0],X[-1])] ) > > Which is pretty much our 'for' loop above, written in > a shorthand single line form. > > hth > > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print certain elements
If you are playing around at the Python prompt (the >>>), which you really should be to get the hang of this stuff, you might notice that the bracket indexing that you and everyone is talking about works both on strings (Y) and on lists (X: in this case, a list of strings). They may not behave the same way as you expect. Try to imagine what Y[13] or X[3][3] would get you, and why. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print certain elements
On Tue, Jan 21, 2014 at 5:18 AM, Alan Gauld wrote: > > for name in X: > if name not in (X[0], X[-1]): >print name > > For the special case of excluding the first and > last names you could use the [:] notation like > this: > > print X[1:-1] > > But that only works where you want *all* the > names in a sequence between two end points. > > Finally there is a more advanced way of filtering > out items from a list called a list comprehension: > > print ( [name for name in X if name not in (X[0],X[-1])] ) If you're using the `print` function, you can use the * operator to unpack the items of the list. Add the option sep='\n' to print each item on a separate line: items = [name for name in X if name not in (X[0],X[-1])] print(*items, sep='\n') To get the `print` function in 2.x, add the following to the top of your module: from __future__ import print_function ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print certain elements
On 01/21/2014 07:18 AM, Adriansanchez wrote: Hello everyone, I am newbie to Python and programming in general. My question is, given a list: X=['washington','adams','jefferson','madison','monroe'] And a string: Y='washington,adams,jefferson,madison,monroe' Side note: you can generate X automatically from Y: X = Y.split(",") print(X) split (as name says) splits a string into a list of substrings. The parameter is the separator separating the substrings. In standard (default value), meaning if you don't indicate a separator, python uses any whitespace (space, tab, newline) possibly repeted. Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print certain elements
On 21/01/14 06:18, Adriansanchez wrote: Hello everyone, I am newbie to Python and programming in general. My question is, given a list: X=['washington','adams','jefferson','madison','monroe'] And a string: Y='washington,adams,jefferson,madison,monroe' How would I print washington and monroe using [:]? How would I print every element but those two names? The [:] syntax is used for selecting a range of values from a starting point to a finish. Its not appropriate for selecting arbitrary items out of the list. If you know which items you want you can use a simple index to access them (remember the first item is index 0) So to print the first item and the fourth item: print(X[0],X[3]) In your case it's the first and last so we can do a similar thing: print(X[0], X[4]) But for the last element we can alternatively use a shortcut to save counting the indexes; that's use an index of -1: print(X[0],X[-1]) Printing every element except those two is harder. The simplest approach is to use a loop to process the list and test each value: for name in X: if name not in (X[0], X[-1]): print name For the special case of excluding the first and last names you could use the [:] notation like this: print X[1:-1] But that only works where you want *all* the names in a sequence between two end points. Finally there is a more advanced way of filtering out items from a list called a list comprehension: print ( [name for name in X if name not in (X[0],X[-1])] ) Which is pretty much our 'for' loop above, written in a shorthand single line form. hth Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to print certain elements
Hello everyone, I am newbie to Python and programming in general. My question is, given a list: X=['washington','adams','jefferson','madison','monroe'] And a string: Y='washington,adams,jefferson,madison,monroe' How would I print washington and monroe using [:]? How would I print every element but those two names? Thanks community! -A- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor