Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > stripping parts of elements in a list

Reply
Thread Tools

stripping parts of elements in a list

 
 
CSUIDL PROGRAMMEr
Guest
Posts: n/a
 
      10-28-2006
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??

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      10-28-2006
"CSUIDL PROGRAMMEr" <(E-Mail Removed)> writes:
> ['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
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-30-2006
CSUIDL PROGRAMMEr wrote:
> 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...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stripping parts of a path Tim Cook Python 3 07-27-2008 08:23 AM
Difference between "library parts" of C99 and "language parts" of C99 albert.neu@gmail.com C Programming 3 03-31-2007 08:14 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Parts parts....PARTS!!! ARGHHH dstvns A+ Certification 8 01-07-2004 07:57 PM
How come most multi parts dont have all parts? 123 Computer Support 2 09-22-2003 01:55 PM



Advertisments