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

M

mrstephengross

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
 
D

Dan

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
 
B

Ben C

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.
 
P

Paulo da Costa

mrstephengross said:
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
 
D

D'Arcy J.M. Cain

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]
 
M

mrstephengross

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

Awesome!

Thanks,
--Steve
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top