Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate multiple ranges in a list [duplicate]

Tags:

python

in python is there a way to create of list that will skip numbers and will continue after skipping? something like the following code:

x = [1...3, 6...10]
print(x)
# [1,2,3,6,7,8,9,10]

Well its easy to write a for loop and then skip each defined index/value, or i can just use range, what I am looking for is a shorter more readable line. If not I can understand.

like image 340
Led Avatar asked Apr 12 '19 15:04

Led


People also ask

How to combine or concatenate ranges in Excel?

To combine or concatenate ranges in Excel with a formula, you can use a formula based on the INDEX function and the SEQUENCE function . In the example below, the formula in cell F5 is: where range1 (B5:B8) and range2 (D5:D9) are named ranges. The formula appends the second range to the first range and the result spills into F5:F13.

How to concatenate lists in Python?

List Concatenation can also be done using the list comprehension technique. In this case, a new list is created, this method is a one-liner alternative to the loop method. The extend () is the function extended by lists in Python language and hence can be used to perform list concatenation operation.

How do I concatenate a range with space as the separator?

Here are the steps to concatenate an Excel Range with space as the separator (as shown in the pic): Select the cell where you need the result. Based on your regional settings, you can also try =A1:A5 (instead of =TRANSPOSE (A1:A5)). Select the entire formula and press F9 (this converts the formula into values).

Can I use the concatenate function with multiple cells at once?

While you can enter the reference one by one within the CONCATENATE function, it would not work if you enter the reference of multiple cells at once (as shown below): For example, in the example above, while the formula used is =CONCATENATE (A1:A5), the result only shows ‘Today’ and doesn’t combine all the cells.


1 Answers

Simplest way to do this is to call range() and unpack result inside list assignment.

x = [*range(1, 4), *range(6, 11)]
like image 103
Olvin Roght Avatar answered Oct 29 '22 02:10

Olvin Roght