Python shortcut ?

S

Santanu Chatterjee

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[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
 
S

Santanu Chatterjee

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
 
D

David Eppstein

"Santanu Chatterjee said:
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[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.
 
S

Santanu Chatterjee

Santanu Chatterjee said:
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[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
 
S

Santanu Chatterjee

Santanu Chatterjee said:
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[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top