You can use list indexes:
>>> mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
>>> mylist[1][1] = 'new_value'
>>> mylist
[['Bob\n', 0], ['Joe\n', 'new_value'], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
In
mylist, in 2nd element (1), in 2nd element of sublist, I set'new_value'
And for \n, you can use str.strip:
>>> mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
>>> mylist = [[i.strip(), j] for i, j in mylist]
>>> mylist
[['Bob', 0], ['Joe', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
Answer from Zulu on Stack OverflowUse a list-comprehension and apply
stripto 1st element of all sublists.
You can use list indexes:
>>> mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
>>> mylist[1][1] = 'new_value'
>>> mylist
[['Bob\n', 0], ['Joe\n', 'new_value'], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
In
mylist, in 2nd element (1), in 2nd element of sublist, I set'new_value'
And for \n, you can use str.strip:
>>> mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
>>> mylist = [[i.strip(), j] for i, j in mylist]
>>> mylist
[['Bob', 0], ['Joe', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
Use a list-comprehension and apply
stripto 1st element of all sublists.
it wont let me comment, but I wanted to add to zulu's answers:
>>>mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
#so far so good, but change mylist[1][0] to mylist[1][1]
>>>mylist[1][1] = 1
>>>mylist
[['Bob\n', 0], ['Joe\n', 1], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
you can access an element of a list using [int] so, mylist[1] is the element of mylist. mylist[1] also happens to be a list. lets say this (assume I didnt change the value yet):
>>>inside_list = mylist[1]
>>>inside_list
['Joe\n', 0]
now I can access this inside list. 0 is the number i want to replace, at index 1, so we can do this:
>>>inside_list[1] = 1
>>>inside_list
['Joe\n', 1]
as a side note, since 0 is an integer, you can also do this:
>>>inside_list[1]+=1
>>>inside_list
['Joe\n', 1]
l.insert(index, obj) doesn't actually return anything. It just updates the list.
As ATO said, you can do b = a[:index] + [obj] + a[index:].
However, another way is:
a = [1, 2, 4]
b = a[:]
b.insert(2, 3)
Most performance efficient approach
You may also insert the element using the slice indexing in the list. For example:
>>> a = [1, 2, 4]
>>> insert_at = 2 # Index at which you want to insert item
>>> b = a[:] # Created copy of list "a" as "b".
# Skip this step if you are ok with modifying the original list
>>> b[insert_at:insert_at] = [3] # Insert "3" within "b"
>>> b
[1, 2, 3, 4]
For inserting multiple elements together at a given index, all you need to do is to use a list of multiple elements that you want to insert. For example:
>>> a = [1, 2, 4]
>>> insert_at = 2 # Index starting from which multiple elements will be inserted
# List of elements that you want to insert together at "index_at" (above) position
>>> insert_elements = [3, 5, 6]
>>> a[insert_at:insert_at] = insert_elements
>>> a # [3, 5, 6] are inserted together in `a` starting at index "2"
[1, 2, 3, 5, 6, 4]
To know more about slice indexing, you can refer: Understanding slice notation.
Note: In Python 3.x, difference of performance between slice indexing and list.index(...) is significantly reduced and both are almost equivalent. However, in Python 2.x, this difference is quite noticeable. I have shared performance comparisons later in this answer.
Alternative using list comprehension (but very slow in terms of performance):
As an alternative, it can be achieved using list comprehension with enumerate too. (But please don't do it this way. It is just for illustration):
>>> a = [1, 2, 4]
>>> insert_at = 2
>>> b = [y for i, x in enumerate(a) for y in ((3, x) if i == insert_at else (x, ))]
>>> b
[1, 2, 3, 4]
Performance comparison of all solutions
Here's the timeit comparison of all the answers with list of 1000 elements on Python 3.9.1 and Python 2.7.16. Answers are listed in the order of performance for both the Python versions.
Python 3.9.1
My answer using sliced insertion - Fastest ( 2.25 µsec per loop)
python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]" 100000 loops, best of 5: 2.25 µsec per loopRushy Panchal's answer with most votes using
list.insert(...)- Second (2.33 µsec per loop)python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)" 100000 loops, best of 5: 2.33 µsec per loopATOzTOA's accepted answer based on merge of sliced lists - Third (5.01 µsec per loop)
python3 -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]" 50000 loops, best of 5: 5.01 µsec per loopMy answer with List Comprehension and
enumerate- Fourth (very slow with 135 µsec per loop)python3 -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]" 2000 loops, best of 5: 135 µsec per loop
Python 2.7.16
My answer using sliced insertion - Fastest (2.09 µsec per loop)
python -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]" 100000 loops, best of 3: 2.09 µsec per loopRushy Panchal's answer with most votes using
list.insert(...)- Second (2.36 µsec per loop)python -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)" 100000 loops, best of 3: 2.36 µsec per loopATOzTOA's accepted answer based on merge of sliced lists - Third (4.44 µsec per loop)
python -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]" 100000 loops, best of 3: 4.44 µsec per loopMy answer with List Comprehension and
enumerate- Fourth (very slow with 103 µsec per loop)python -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]" 10000 loops, best of 3: 103 µsec per loop
Videos
say i have array [[1,2,3,4,5]], how can i duplicate it 5 times to [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] ?
So if I have
x = [1, 2, 3] then x.append(4) will be [1, 2, 3, 4]
However, the code itself is unaffected by the code. So that's what I'm looking for right now. Possible how to write to an array in another file.
THANKS!