Linked Questions

10 votes
3 answers
50k views

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, ...
iKyriaki's user avatar
  • 619
3 votes
5 answers
8k views

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 ...
user3079411's user avatar
2 votes
2 answers
2k views

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 ...
airzinger1's user avatar
3 votes
1 answer
4k views

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, ...
Nicolas O's user avatar
  • 151
-4 votes
2 answers
2k views

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 ...
Inzu Desu's user avatar
-1 votes
1 answer
892 views

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?
Ffr Drr's user avatar
0 votes
2 answers
861 views

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", "...
Hades's user avatar
  • 11
0 votes
1 answer
657 views

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 ...
infoclogged's user avatar
  • 4,101
0 votes
2 answers
330 views

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]...
max's user avatar
  • 79
-1 votes
1 answer
76 views

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]
user5960525's user avatar
0 votes
2 answers
91 views

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 ...
Pierre's user avatar
  • 41
4708 votes
38 answers
3.3m views

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 ...
Simon's user avatar
  • 81.4k
9 votes
2 answers
22k views

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 ...
Coloane's user avatar
  • 317
3 votes
5 answers
21k views

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]
bloomwoood's user avatar
4 votes
5 answers
5k views

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':...
cooldood3490's user avatar
  • 2,528

15 30 50 per page