0

Let's say we have an existing empty list, m. I would like to append some elements (let's say three 7's) to m so that m is now [7,7,7]. How to do this using list iterators?

I have the following solution, which is not complete:

m = []
m.append([7 for i in range(3)])

I get m = [[7,7,7]] but not [7,7,7].

I am not looking for post-processing like making m flat such as: flat_list = [item for sublist in l for item in sublist], because it is an extra step.

1 Answer 1

1

You may want to try extend rather than append.

m = []
m.extend([7 for i in range(3)])

Here is a post that may be helpful

Sign up to request clarification or add additional context in comments.

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.