Remove every third element from a list [duplicate] - python

This question already has answers here:
How to remove/delete every n-th element from list?
(5 answers)
Closed 4 years ago.
In this code piece I print the index numbers in a list.
x = '1 2 3 4 67 8'
x = list(map(int, x.split()))
# something here with modulo??
print(x)
Output:
[1, 2, 3, 4, 67, 8]
I want to output
[1, 2, 4, 67]
So the 3th element, 6th, 9th etc...

Iterate the list and keep index value which is not multiple of 3
[value for key,value in enumerate(x,1) if key%3!=0 ]

Related

Iterate over multiple elements at once Python [duplicate]

This question already has answers here:
Iterate an iterator by chunks (of n) in Python?
(14 answers)
How to iterate over a list in chunks
(39 answers)
Closed 3 months ago.
If I have a list like l=[1,2,3,4,5,6]. If I do something like:
for i in l:
print(i)
It will print all element separately.
1
2
3
4
.
.
.
Is there a way to iterate simultaneously over multiple elements?
For example, if I want to iterate over each 3 elements
for ....:
print(..)
Should print:
1,2,3
4,5,6
So that in each iteration the variable that iterate the for will be a tuple of, in this case, 3 elements.
Iterate over indices, and step three at a time, then use a slice within the loop to get the desired values.
>>> l=[1,2,3,4,5,6]
>>> for i in range(0, len(l), 3):
... print(l[i:i+3])
...
[1, 2, 3]
[4, 5, 6]
>>>
To get exactly the required output you could do this:
lst = [1, 2, 3, 4, 5, 6]
for i in range(0, len(lst), 3):
print(*lst[i:i+3], sep=',')
Output:
1,2,3
4,5,6

How do i print a list of string and integers vertically in python [duplicate]

This question already has answers here:
String formatting: Columns in line
(4 answers)
Closed 1 year ago.
i am trying to print out a list of string and integers vertically.
map = [["SG", 8], ["MY", 8], ["PH", 8], ["ID", 8], ["TH", 8]]
the print should return:
SG 8
MY 8
PH 8
ID 8
TH 8
this is what i have :
for element in map:
print(element)
the failed output:
['SG', 8]
['MY', 8]
etc....
May note be the best solution, but try this.
for element in map:
print("{}\t{}".format(element[0],element[1]))

Get ascending order of numbers in array in python [duplicate]

This question already has answers here:
Translate integers in a numpy array to a contiguous range 0...n
(2 answers)
Closed 4 years ago.
I have a Numpy array with some numbers and I would like to get order the items ascending order.
For example, I have a list:
[4, 25, 100, 4, 50]
And I would like to use a function to get this:
[1, 2, 4, 1, 3]
Any ideas how to do this?
There is a convenient method via pandas:
import pandas as pd
lst = [4, 25, 100, 4, 50]
res = pd.factorize(lst, sort=True)[0] + 1
# [1 2 4 1 3]

iPython 3.5.2 lists [duplicate]

This question already has answers here:
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 6 years ago.
I have created a list size 12, ex: V[0 1 2 3 4 5 6 7 8 9 10 11]
How do I return only the last 10 digits [2:11]? Also what if the list had n variables, I tried V[2,:], but I get TypeError: list indices must be integers or slices, not tuple.
If you want to get the last x elements of a list, you need to use a negative index in your slice.
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[-5:]
[5, 6, 7, 8, 9]
As for the error you mentioned, it looks like you have a stray comma after the 2, which makes 2 the first element in a tuple. Take out the comma and it should be a valid slice.

python - How to move back an iteration in a for loop list [duplicate]

This question already has answers here:
Making a python iterator go backwards?
(14 answers)
Closed 6 years ago.
Given the list: a = [1, 2, 3, 4 5] in a for loop, suppose that the current item is 2, and next item is 3. If some condition is true, how can I make the next item be 2 again, which means the iteration should continue from 2 again instead of 3?
a = [1, 2, 3, 4, 5]
for item in a:
print item
if condition:
do something, and go back to previous iterator
The output would be:
1
2
2
3
4
5
Beware of an infinite loop, and it's not very Pythonic.
i = 0
a = [1, 2, 3, 4, 5]
hasBeenReset = False
while i < len(a):
if a[i] == 3 and not hasBeenReset:
i = 1
hasBeenReset = True
print(a[i])
i += 1

Categories