3

I looked and couldn't find a similar question probably because I'm a python noob and don't know the proper language to search.

is there a way to do this...

frame_inds = [0,  list(range(200, 2000, 100)), 3999]

the output I get is this

[0, [200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900], 3999]

but I want this

[0, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 3999]

so that it is all in one array?

in matlab you can do this var1 = [1, 2, 3:10:100, 400]

2
  • 2
    These are lists, not arrays. array means something different in Python; usually it's talking about something produced by the third-party Numpy library, although it can also refer to something from the array standard library module. Commented Jul 16, 2020 at 20:46
  • If you're coming from Matlab, you almost certainly should get NumPy, by the way. Commented Jul 16, 2020 at 20:56

2 Answers 2

6
frame_inds = [0,  *range(200, 2000, 100), 3999]

By using the * operator you can unpack all its items and they become items in the main list.

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

5 Comments

You don't need to call list. That just wastes time and space on a temporary list.
@user2357112supportsMonica, True, fixed.
awesome I will accept this answer when it allows me to. need to wait a fe min. I thought list was needed to make it an array of numbers? when is list needed?
@PhillipMaire, range is an iterator, which means that it works for accessing the items one after the other. You will sometimes see it converted to a list when access is needed by index and not in order, or when you need it more than once (so it's better to create the list once rather than the iterator several times). In this case the * operator iterates over the range, so no need to make it a list first.
range isn't actually an iterator. It's a lazy sequence type that generates elements on demand. (Try indexing a range. It works!)
2

I would simply concatenate the lists, using the + operator.

frame_inds = [0]+list(range(200, 2000, 100))+[3999]

1 Comment

This will not be efficient, you are creating 5 lists needlessly (4 temporary, because adding two temporaries creates another temporary one, and then the final one)

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.