This question already has answers here:
Understanding slicing
(38 answers)
Closed 7 months ago.
I am trying to slice a Python list and run into the following error:
>>> a=[2,3,4,5,6,7,8,9]
>>> print(a)
[2, 3, 4, 5, 6, 7, 8, 9]
>>> print(a[0:2])
[2, 3]
>>> print(a[2:2])
[]
>>>
Expected answer: [4,5]
Could someone tell me what I am doing wrong?
EDIT: I figured out my own answer here.
>>> a=[2,3,4,5,6,7,8,9]
>>> print(a)
[2, 3, 4, 5, 6, 7, 8, 9]
>>> print(a[0:2])
[2, 3]
>>> print(a[2:4])
[4, 5]
>>>
n00b mistake!
print(a[2:4])
The second number is end index, not slice length.
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
list[0:9] and list[1:10] were showing same in my python IDLE. I'm not sure the logic behind, could some one please explain. Please see the values retrieved below.
>>> list[0:9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list[0:10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In the latter the second index is over the last element.
l = [1,2,3]
l[0:100]
=> [1,2,3]
This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 5 years ago.
x = [4, 5, 6]
li = [1, 2, 3, 7]
li.insert(3,x)
x+=li
print(x)
The output is:
[4, 5, 6, 1, 2, 3, [...], 7]
I'm new to python/coding and I don't know what these ellipses are but when I do other code it starts getting weird. Wasn't sure what to ask since I have no clue what's going on. Thank you!
you're inserting a list inside your list, probably not what you want.
Then when doing this
x+=li
the representation of the list then shows an ellipsis because you're referencing the list from itself (x is referenced in li already)
To insert several items at once in a list in-place you could use slice assignment:
>>> x = [4, 5, 6]
>>> li = [1, 2, 3, 7]
>>> li[3:3] = x
>>> li
[1, 2, 3, 4, 5, 6, 7]
This question already has answers here:
Rolling or sliding window iterator?
(29 answers)
Closed 6 years ago.
I would like to reorder a python list:
a = [1,2,3,4,5,6,7,...]
to the following form:
[[1,2,3],[2,3,4],[3,4,5],...]
What is the fastest way to do that?
you can try:
>>> a = [1,2,3,4,5,6,7]
>>> new_list = []
>>> for index in range(len(a)-2):
new_list.append(a[index:index+3])
>>> new_list
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
This question already has answers here:
Element-wise addition of 2 lists?
(17 answers)
Closed 7 years ago.
I'm sure there is a good way to accomplish what i want without looping over lists and creating new objects. Here is what I have
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
What I am looking to do is take each set of lists and sum each placeholder so that the output is
[3, 5, 7, 9]
Thoughts?
you should use zip function and list comprehension
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
[sum(t) for t in zip(a,b)]
Use numpy
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([2, 3, 4, 5])
a+b
>>> array([3, 5, 7, 9])
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 9 years ago.
I have one list like:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
I want to create a function that takes a single list (see above) and concatenates all the sublists that are part of it into a single list.
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
nn = [ x for y in n for x in y]
>>> lst = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
>>> from itertools import chain
>>> list(chain.from_iterable(lst))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
For completeness, here is a very short way to write this
>>> sum(n, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
But although it is tempting, you shouldn't because it has quadratic performance. ie a new list is created as each term is added, and all the previous items will be copied over and over
It's ok to use list.extend though
reduce(lambda x,y: x.extend(y) or x, n, [])
You can also concatenate by doing simply:
print n[0]+n[1]
In general this would be:
def concatenate(list):
x=[]
for i in list:
x+=i
return x
But this is not particularly efficent, just quite straightforward for a beginner.