Hello everybody,
I am very new to Python. So pardon me if this question is
too dumb.
Suppose I have the following list:
myList = [('a','hello), ('b','bye')]
How do I get only the first element of each tuple in the
above list to be printed without using:
for i in range(len(myList)):
print myList[i][0]
or:
for i in myList:
print i[0]
I tried:
print myList[:][0]
but it seems to have an altogether different meaning.
Regards,
Santanu 4 1818
Please ignore this message. It had been written much before
my other thread on 'A Query about List' and for some reason
had stayed in the 'send later' folder of Pan, and accidentally
got posted today.
Sorry.
Regards,
Santanu
In article <pa****************************@softhome.net>,
"Santanu Chatterjee" <sa*****@softhome.net> wrote: Suppose I have the following list: myList = [('a','hello), ('b','bye')]
How do I get only the first element of each tuple in the above list to be printed without using: for i in range(len(myList)): print myList[i][0] or: for i in myList: print i[0]
I tried: print myList[:][0] but it seems to have an altogether different meaning.
If you're set on a one-liner, you could try
print zip(*myList)[1]
or
print [word for letter,word in myList]
These are no quite the same -- zip makes tuples, the list comprehension
makes a list. My own preference would be for the list comprehension --
it's not as concise, but the meaning is clearer.
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
On Sun, 12 Oct 2003 13:21:21 -0700, David Eppstein wrote: In article <pa****************************@softhome.net>, "Santanu Chatterjee" <sa*****@softhome.net> wrote:
Suppose I have the following list: myList = [('a','hello), ('b','bye')]
How do I get only the first element of each tuple in the above list to be printed without using: for i in range(len(myList)): print myList[i][0]
I tried: print myList[:][0] but it seems to have an altogether different meaning.
If you're set on a one-liner, you could try print zip(*myList)[1] or print [word for letter,word in myList]
These are no quite the same -- zip makes tuples, the list comprehension makes a list. My own preference would be for the list comprehension -- it's not as concise, but the meaning is clearer.
Thanks for the solution. I was already using the second solution.
I did not know about the zip function.
Regards,
Santanu
On Sun, 12 Oct 2003 13:21:21 -0700, David Eppstein wrote: In article <pa****************************@softhome.net>, "Santanu Chatterjee" <sa*****@softhome.net> wrote:
Suppose I have the following list: myList = [('a','hello), ('b','bye')]
How do I get only the first element of each tuple in the above list to be printed without using: for i in range(len(myList)): print myList[i][0] or: for i in myList: print i[0]
I tried: print myList[:][0] but it seems to have an altogether different meaning.
If you're set on a one-liner, you could try print zip(*myList)[1] or print [word for letter,word in myList]
These are no quite the same -- zip makes tuples, the list comprehension makes a list. My own preference would be for the list comprehension -- it's not as concise, but the meaning is clearer.
Thanks for the solution. I was already using the second solution.
I did not know about the zip function.
Regards,
Santanu This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: Brandon J. Van Every |
last post by:
I'm realizing I didn't frame my question well.
What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump
up in your chair and scream "Wow! Ruby has *that*? That is SO...
|
by: DavidHolt |
last post by:
I have a problem that I see on two different machines, one running XP
SP1 and one XP SP 2.
On both I installed Python 2.4.
I can't seem to start IDLE. When I try to start it, I get an...
|
by: Mike Moum |
last post by:
Hi,
I'm a civil engineer who also doubles as chief programmer for technical
applications at my company. Most of our software is written in Visual
Basic because our VP in charge of I.T. likes to...
|
by: Paul Rubin |
last post by:
Dumb question from a Windows ignoramus:
I find myself needing to write a Python app (call it myapp.py) that
uses tkinter, which as it happens has to be used under (ugh) Windows.
That's Windows...
|
by: n.estner |
last post by:
Having read previous discussions on python-dev I think I'm not the only
Python programmer who doesn't particularly like python's "self"
parameter:
class Foo:
def bar(self, a,b):
return a+b...
|
by: Ben Sizer |
last post by:
I'd like to be able to drag a file onto a Python script in Windows
Explorer, or send that file to the script via the Send To context-menu
option, so I can then process that file via sys.argc.
...
|
by: Jeremy Sanders |
last post by:
Here is a brief simple introduction to Python I wrote for a computing course
for graduate astronomers. It assumes some programming experience. Although
it is not a complete guide, I believe this...
|
by: Stef Mientki |
last post by:
hello,
I'm currently using Python 2.4,
and I don't dare to switch to 2.5,
because I depend heavily on Scipy, which is based on 2.4
To test some other Python programs I need Python version...
|
by: Jorgen Bodde |
last post by:
Hi all,
This is slightly OT but it drives me nuts. Whenever I create a
shortcut in the start menu (in Windows) of a python script, it will
only execute it when the path where the script resides...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |