Iterate over multiple elements at once Python [duplicate] - python

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

Related

Adding amount of elements in nested lists [duplicate]

This question already has answers here:
Count all elements in list of arbitrary nested list without recursion
(3 answers)
Nested List and count()
(8 answers)
Closed 11 months ago.
I have this problem, I want to count how many integers in a list AND sub-lists.
I have this code but doesn't seem to be working:
x = [[1, 3], [0], [2, -4]]
count = 0pos = 0
for i in x[pos]:count = count + len(x[pos])pos = pos + 1
print(count)
Expected 5 because there are 5 elements.
I've edited your code to the following:
x = [[1, 3], [0], [2, -4]]
count = 0
for arr in x:
for element in arr:
count +=1
print(count)

Remove every third element from a list [duplicate]

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 ]

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: for-loop wrong output [duplicate]

This question already has answers here:
Removing elements from a List in Python
(3 answers)
Closed 6 years ago.
Can't understand why it missing some indexes in loop?
Index number 3 for example.
I just want to delete the unique elements this way. I know another one but I don't know what's wrong with this one (or me).
lst = [2, 7, 1, 9, 3, 5, 2, 1]
def check(lst):
result = lst
print(lst) #checking
for num in lst:
print("index:", num) #checking
print("num:", num) #checking
if lst.count(num) < 2:
print("count:", lst.count(num)) #checking
result.remove(num)
return(result)
print(check(lst))
Output
[2, 7, 1, 9, 3, 5, 2, 1]
index: 2
num: 2
index: 7
num: 7
count: 1
index: 9
num: 9
count: 1
index: 5
num: 5
count: 1
index: 1
num: 1
[2, 1, 3, 2, 1]
You're removing items from a list as you're iterating over it, which you shouldn't do.
(result = lst does NOT make a copy of lst; it creates a new name which refers to the same object.)
If you just want to remove duplicate elements, you can use a set (although you may lose your ordering):
lst = list(set(lst))

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