The c
function in R is a function which concatenates its arguments as lists. You can concatenate two lists in Python using +
; to concatenate a variable number of lists, as c
in R does, you can use the sum
function. This normally adds numbers, but can be used to add lists by "starting" from an empty list instead of from 0:
>>> lists = [[1,2,3], [4,5,6], [7,8,9]]
>>> sum(lists, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
However, this is unrelated to the error in your code. The problem is that in Python, you can only use the syntax mylist[i] = x
to set a new value at an index which already exists. Since you created an empty list, it has no elements so the index 2 is out of bounds.
If you want to be able to set values at arbitrary indices, which don't necessarily form a contiguous range, you could use a dictionary:
>>> q = dict()
>>> q[2] = 9
However, there will be a mismatch between what some operations do and what you expect them to do for a list-with-some-unset-values; for example, len(q)
gives the number of elements which are set, rather than the length of the sequence including the unset values.