mylist=[]
mylist.append(7)
mylist.extend(range(9,12))
can such a thing be done in a single line in python3?
I feel it should be trivial, but for some reason I can't recall nor find how to do that.
mylist=[]
mylist.append(7)
mylist.extend(range(9,12))
can such a thing be done in a single line in python3?
I feel it should be trivial, but for some reason I can't recall nor find how to do that.
You can use this one liner:
mylist = [7] + list(range(9,12))
It returns the desired list:
[7, 9, 10, 11]
You can add whatever you want to the list constructor. For example:
mylist = [7]
Or:
mylist = list(range(9,12))
or, to chain the two together:
mylist = [7] + list(range(9,12))
For more complex constructions, list comprehensions are the way to go. For example:
mylist = [ (irow,icol) for irow in range(1,10) for irow in range(1,10) if i > j ]
More information on list comprehensions is available at: http://docs.python.org/tutorial/datastructures.html#list-comprehensions
(Edited to reflect the change in the question).
If you are using Python 2.* as others have said
for lid in [7] + range(9,len(row)):
will work
if working with Python 3.*, as range now returns an iterator you need to explicitly convert to list
Option 1:
for lid in [7] + list(range(9,len(row))):
Option 2:
for lid in itertools.chain([7],range(9,len(row))):