stripping parts of elements in a list

Discussion in 'Python' started by CSUIDL PROGRAMMEr, Oct 28, 2006.

  1. 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??
     
    CSUIDL PROGRAMMEr, Oct 28, 2006
    #1
    1. Advertising

  2. CSUIDL PROGRAMMEr

    Paul Rubin Guest

    "CSUIDL PROGRAMMEr" <> 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
     
    Paul Rubin, Oct 28, 2006
    #2
    1. Advertising

  3. 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 ''.split('@')])"
     
    Bruno Desthuilliers, Oct 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    611
    Keith Thompson
    Mar 31, 2007
  2. Replies:
    1
    Views:
    964
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 12, 2007
  3. Tim Cook

    Stripping parts of a path

    Tim Cook, Jul 26, 2008, in forum: Python
    Replies:
    3
    Views:
    200
    Tim Cook
    Jul 27, 2008
  4. kizk
    Replies:
    0
    Views:
    629
Loading...

Share This Page