0

Is there a way to create a list using both strings and a generator (for loop).

list() only takes one argument. The only way I can see so far is:

row = ['string1','string2']+list((t.string3[-7:]) for t in tobject)

What am I missing?

1
  • Well I mean... what are you hoping it might look like? What are you hoping to gain compared to what you have now? Commented Jul 23, 2012 at 14:11

3 Answers 3

4

Do you need to do it in only one line? chain() works, but it looks so unreadable to me. The following is probably less elegant, but I think it's far more readable:

row = ['string1', 'string2']
row.extend(t.string3[-7:] for t in tobject)
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, in this particular case, I find this more elegant than chain.
What makes this less elegant? Sometimes 2 lines are better than 1. (+1 from me)
2
import itertools
list(itertools.chain(('string1','string2'), (t.string3[-7:] for t in tobject)))

Comments

1

No, you have to concatenate two lists.

You may prefer to chain the iterables and then call list on them once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.