How to manipulate elements of a list in a single line of code?

Discussion in 'Python' started by mrstephengross, Feb 25, 2008.

  1. I would like to translate the contents of a list. For instance, let's
    say I've got a list of strings and I want to append "foo" to each
    element. I might do the following;

    list1 = ['a', 'b', 'c']
    for i in range(0, len(list1)): list1 += 'foo'

    Ok, that much works. But what if I don't want to modify the contents
    of list1. Instead, I want list2 to hold the modified contents, like
    so:

    1 list1 = ['a', 'b', 'c']
    2 list2 = []
    3 for item in list1: list2.append(item + 'foo')

    Is there a way to express lines 2-3 sort-of ilke this:

    list2 = [ for item in list1: item + 'foo' ]

    Any ideas?

    Thanks,
    --Steve
     
    mrstephengross, Feb 25, 2008
    #1
    1. Advertising

  2. mrstephengross

    Dan Guest

    On Feb 25, 4:39 pm, mrstephengross <> wrote:
    > I would like to translate the contents of a list. For instance, let's
    > say I've got a list of strings and I want to append "foo" to each
    > element. I might do the following;
    >
    > list1 = ['a', 'b', 'c']
    > for i in range(0, len(list1)): list1 += 'foo'
    >
    > Ok, that much works. But what if I don't want to modify the contents
    > of list1. Instead, I want list2 to hold the modified contents, like
    > so:
    >
    > 1 list1 = ['a', 'b', 'c']
    > 2 list2 = []
    > 3 for item in list1: list2.append(item + 'foo')
    >
    > Is there a way to express lines 2-3 sort-of ilke this:
    >
    > list2 = [ for item in list1: item + 'foo' ]
    >
    > Any ideas?
    >
    > Thanks,
    > --Steve


    You're so close.

    >>> list2 = [ item+"foo" for item in list1 ]


    -Dan
     
    Dan, Feb 25, 2008
    #2
    1. Advertising

  3. mrstephengross

    Ben C Guest

    On 2008-02-25, mrstephengross <> wrote:
    > I would like to translate the contents of a list. For instance, let's
    > say I've got a list of strings and I want to append "foo" to each
    > element. I might do the following;
    >
    > list1 = ['a', 'b', 'c']
    > for i in range(0, len(list1)): list1 += 'foo'
    >
    > Ok, that much works. But what if I don't want to modify the contents
    > of list1. Instead, I want list2 to hold the modified contents, like
    > so:
    >
    > 1 list1 = ['a', 'b', 'c']
    > 2 list2 = []
    > 3 for item in list1: list2.append(item + 'foo')
    >
    > Is there a way to express lines 2-3 sort-of ilke this:
    >
    > list2 = [ for item in list1: item + 'foo' ]


    Yes, it's called a "list comprehension", and is many people's favourite
    Python feature.

    list2 = [x + 'foo' for x in list1]

    You can also add a condition

    list2 = [x + 'foo' for x in list1 if x != "bar"]

    for example.
     
    Ben C, Feb 25, 2008
    #3
  4. mrstephengross wrote:
    > 1 list1 = ['a', 'b', 'c']
    > 2 list2 = []
    > 3 for item in list1: list2.append(item + 'foo')
    >
    > Is there a way to express lines 2-3 sort-of ilke this:
    >
    > list2 = [ for item in list1: item + 'foo' ]


    list2 = [ item + 'foo' for item in list1 ]

    Paulo
     
    Paulo da Costa, Feb 25, 2008
    #4
  5. On Mon, 25 Feb 2008 13:39:26 -0800 (PST)
    mrstephengross <> wrote:
    > 1 list1 = ['a', 'b', 'c']
    > 2 list2 = []
    > 3 for item in list1: list2.append(item + 'foo')
    >
    > Is there a way to express lines 2-3 sort-of ilke this:
    >
    > list2 = [ for item in list1: item + 'foo' ]


    You almost have it.

    list2 = [item + 'foo' for item in list1]

    --
    D'Arcy J.M. Cain <> | Democracy is three wolves
    http://www.druid.net/darcy/ | and a sheep voting on
    +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
     
    D'Arcy J.M. Cain, Feb 25, 2008
    #5
  6. > Yes, it's called a "list comprehension", and is many people's favourite

    Awesome!

    Thanks,
    --Steve
     
    mrstephengross, Feb 25, 2008
    #6
    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. Dave
    Replies:
    1
    Views:
    332
    Mike Wahler
    Jan 22, 2005
  2. Adam Hartshorne
    Replies:
    2
    Views:
    376
    Nitin Motgi
    Jan 27, 2006
  3. Debajit Adhikary
    Replies:
    17
    Views:
    693
    Debajit Adhikary
    Oct 18, 2007
  4. Paul
    Replies:
    5
    Views:
    132
    Peter Szinek
    Apr 13, 2007
  5. ela
    Replies:
    12
    Views:
    353
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page