stripping parts of elements in a list

C

CSUIDL PROGRAMMEr

folks,
I am new to python.

I have a list made of elements

['amjad\n', 'kiki\n', 'jijiji\n']
I am trying to get rid of '\n' after each name.
to get list as
['amjad','kiki','jijiji']

But list does not have a strip function as string does have.

is there any solutions

Is there a way this can be done??
 
P

Paul Rubin

CSUIDL PROGRAMMEr said:
['amjad\n', 'kiki\n', 'jijiji\n']
I am trying to get rid of '\n' after each name.
to get list as
['amjad','kiki','jijiji']

But list does not have a strip function as string does have.

is there any solutions

a = ['amjad\n', 'kiki\n', 'jijiji\n']

b = [x.strip() for x in a]

print b
 
B

Bruno Desthuilliers

CSUIDL said:
folks,
I am new to python.

I have a list made of elements

['amjad\n', 'kiki\n', 'jijiji\n']
I am trying to get rid of '\n' after each name.
to get list as
['amjad','kiki','jijiji']

But list does not have a strip function as string does have.

What would a list.strip() method mean on a list of integers ?
is there any solutions

mylist = ['amjad\n', 'kiki\n', 'jijiji\n']
print "with map : "
print map(str.lstrip, mylist)
print "with list comprehension :"
print [line.lstrip() for line in mylist]
print "with a for loop :"
strippedlist = []
for line in mylist:
strippedlist.append(line.lstrip())
print strippedlist
Is there a way this can be done??

Probably. Reading some CS101 tutorial might be a good idea...
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top