Linked Questions
24 questions linked to/from Make a new list containing every Nth item in the original list
10
votes
3
answers
50k
views
Finding every nth element in a list [duplicate]
How can I find every nth element of a list?
For a list [1,2,3,4,5,6], returnNth(l,2) should return [1,3,5] and for a list ["dog", "cat", 3, "hamster", True], returnNth(u,2) should return ['dog', 3, ...
3
votes
5
answers
8k
views
Get every third element from a list [duplicate]
For example if I have a list like the following
lst = [1, 10, 100, 2, 20, 200, 3, 30, 300];
How could I get back [1, 2, 3]? Basically it would be starting from the first number, and every third ...
2
votes
2
answers
2k
views
Only create list in python with every 5th item? [duplicate]
I have a list of 20 items. I want to create a new list than only contains every 5th item of the list.
Here is was I have:
listA=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for i in ...
3
votes
1
answer
4k
views
Python: Selecting every Nth row of a matrix [duplicate]
does anyone know how to select multiple rows of a matrix to form a new one - e.g. I would like to select EVERY 3rd row of a matrix and build a new matrix with these rows.
Many thanks for your help,
...
-4
votes
2
answers
2k
views
How to add +1 to every nth number in list, python [duplicate]
I generated list of zeros.
num=raw_input
list=[0]*int(num)
I want every 1st and 2nd number after it in list2 to have +1 added to it.
For every 3rd number in list3, I want to have +1 added to it.
I ...
-1
votes
1
answer
892
views
How can I sum up every second number in list [duplicate]
Given that a list
[10,20,30,40,50,60]
If I want to sum up every second number. For instance 20+40+60=120.
So what should I write in python? Is it while loop or for loop?
0
votes
2
answers
861
views
Delete every other element from a list [duplicate]
I need to take an array and remove every second element from the array. Always keep the first element and start removing with the next element.
Example:
["Keep", "Remove", "...
0
votes
1
answer
657
views
extract every 4th elements from the numpy array [duplicate]
i want to extract every 4th element in the numpy array
The array is
a = [0,1,3,4,5,6,7,8, 9]
final_array shuld be [0,5,9]
The array is ofocurse very huge, so just the indices (0,4,8) would not be ...
0
votes
2
answers
330
views
Arrays selection in python [duplicate]
What is the most efficient way in python to select rows from a matrix with indices divisible by certain number (using numpy, torch or any)?
Example 1:
mat=[[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]...
-1
votes
1
answer
76
views
How to access List elements with specific multiple index [duplicate]
I have a list
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
I need to store every fifth element, with the result:
result=[5,10,15]
0
votes
2
answers
91
views
Get accurate number in list [duplicate]
I search something in python to return a result from a list every 4 number
Here an example:
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
I would like only [1,5,9,13,17]
So every 4 elements of ...
4708
votes
38
answers
3.3m
views
How slicing in Python works
How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice?
See Why are slice and range upper-bound ...
9
votes
2
answers
22k
views
Matplotlib pyplot - tick control and showing date
My matplotlib pyplot has too many xticks - it is currently showing each year and month for a 15-year period, e.g. "2001-01", but I only want the x-axis to show the year (e.g. 2001).
The output will ...
3
votes
5
answers
21k
views
Python - Average every "n" elements in a list
I need to average every n elements in Python list, n = 3 in this example:
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
So that the output list would be:
list2 = [2, 2, 2, 5, 5, 5, 8, 8, 8]
4
votes
5
answers
5k
views
Python: Loop Dictionary, Two keys at a time
I'm working on a program that requires me to loop a dictionary two keys at a time. Here's a simple representation of my dictionary.
my_dict = {
'LAP0-1': 146.48,
'LAP1-1':44.11,
'LAP2-1':...