0

I want to pass a range of numbers to a list. Here I set the variable rowsList which contain a list of index numbers.

rowsList = [range(13),17,23]
for index in rowsList:
    ws.insert_rows(index,1)

Obviously it raises: TypeError: 'range' object cannot be interpreted as an integer. What little change could I do to make this work ? Thanks for your help.

Underoos
  • 4,708
  • 8
  • 42
  • 85
CharlesV
  • 60
  • 8
  • 1
    Your question isn't clear. Can you show us what you're expecting to happen? What is `ws`? – brunns May 09 '19 at 11:21
  • What are you expecting from `rowsList`? – RoadRunner May 09 '19 at 11:21
  • See the answer to [Appending a range to a list](https://stackoverflow.com/questions/42437608/appending-a-range-to-a-list). – evergreen May 09 '19 at 11:24
  • @brunns My question is solved but just so you know, `ws` is the name I gave to my worksheet (Openpyxl library, see tags). I didn't find relevant to mention it :-) – CharlesV May 09 '19 at 13:09
  • You should note that the in order which rows are inserted matters as each insertion moves all the rows below. As it stands inserting `range(13)` makes little sense. – Charlie Clark May 09 '19 at 13:25

2 Answers2

4

* (Extended Iterable Unpacking) operator

Proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.

rowsList = [*range(13),17,23]
print (rowsList)

output :

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]
ncica
  • 7,015
  • 1
  • 15
  • 37
0

Assuming you want [17, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

rowsList = [17, 23]
rowsList.extend(range(13))

Assuming you want [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 23]

rowsList = [17, 23]
tmpList = []
tmpList.extend(range(13))
rowsList[:0] = tmpList
Matt M
  • 691
  • 2
  • 6
  • 17