Why it removes certain items from the list? [duplicate] - python

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 2 years ago.
I am new to python and i was just writing a programm to get an input like :
1 2 3 4 5 6 7 8 9
I wanted to remove the integers which are bigger than 2 and this was my code:
a = input()
b =a.split()
for i in range(len(b)):
b[i] = int(b[i])
for i in b:
if i >=3:
b.remove(i)
print(b)
I expected an output like this :
[1,2]
but when I run it,it shows this :
[1, 2, 4, 6, 8]
Can someone tell me ,where I made mistake?

Check with below code:
#a = '1 2 3 4 5 6 7 8 9'
a = input()
b =a.split()
for i in range(len(b)):
b[i] = int(b[i])
tempB = []
for i in b:
if i < 3:
tempB.append(i)
print(tempB)
More optimized and shortcode:
b =list(map(int,input().split()))
tempB = [i for i in b if i < 3]
print(tempB)

Related

how can i control the iteration of the for loop in python [duplicate]

This question already has answers here:
Changing the value of range during iteration in Python
(4 answers)
Closed 2 years ago.
I am coming from a c++ language and there if i change the value of 'i' in the loop then it affect the iteration of the loop but it is not happening in python.
for example, in c++:
for(int i=0; i<10; i++){
cout<<i<<" ";
if(i==5)
i = 8;
}
in above code, as the value of 'i' reaches 5 it become 8 and after one more iteration it become 9 and then loop ends.
output of the above code is-
0 1 2 3 4 5 9
but when i write the similar code in python it doesn't affect the iteration and the loop runs all 10 times.
code in python is -
for i in range(0, 10):
if i == 5:
i = 8
print(i, end=" ")
the output of this code is -
0 1 2 3 4 8 6 7 8 9
it is just changing the value of 5 to 8, and not changing the loop iteration.
how can i achieve the c++ result in python, please help!
thanks in advance :)
When you say i in range(0,10) you are generating a list of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] that you are later on iterating over, this means that the value of i is not used as an increasing int but you are rather iterating over an array.
The best solution would probably be to change from a for loop to a while loop instead.
i = 0
while i < 10:
if i == 5:
i = 8
print(i)
i += 1
You could try a while loop:
i = 0
while i < 10:
if i == 6:
i = 9
print(i)
i += 1
Output:
0 1 2 3 4 5 9

Recursive one liner function to print numbers from N to 1 [duplicate]

This question already has answers here:
Can a lambda function call itself recursively in Python?
(16 answers)
Closed 2 years ago.
def recur(n):
print(n)
if n>1:
recur(n-1)
How can this be converted to just line. I can not find a way to use the logic as
recur = lambda x: print(x) if.......
I can not even use
func = lambda x: [print(i) for i in range(x,0,-1)]
If you want the numbers printed on the same line, you can use the end= parameter of the print function:
printDown = lambda n: print(n,end=" ") or printDown(n-1) if n>1 else print(n)
printDown(10)
10 9 8 7 6 5 4 3 2 1
If you want them on separate lines:
printDown = lambda n: print(n) or (printDown(n-1) if n>1 else None)
printDown(10)
10
9
8
7
6
5
4
3
2
1
One possible recursive lambda:
recur = lambda n: [print(n), recur if n>1 else lambda x: None][1](n-1)
recur(10)
Prints:
10
9
8
7
6
5
4
3
2
1
In Python >= 3.8, you can do it in one line entirely:
print(*(x := lambda n: [n]+x(n-1) if n else [])(10), sep='\n')
10
9
8
7
6
5
4
3
2
1
Note that this also does not abuse a comprehension or expression for side effects. x can still be used to create a common list now:
x(5)
# [5, 4, 3, 2, 1]

Print 5 elements of a list per line [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 4 years ago.
l = [1,2,3,4,5,6,7,8,9,10]
I have a list and I need to print 5 elements per line to get:
1 2 3 4 5
6 7 8 9 10
I tried everything but I'm stuck, help!
#Joaquin, try below code:
http://rextester.com/BLN7722
l = [1,2,3,4,5,6,7,8,9,10]
for index, item in enumerate(l):
if (index + 1) % 5 == 0:
print(item)
else:
print(item, end=" ")
"""
1 2 3 4 5
6 7 8 9 10
"""

Access an element in List 2D in Python [duplicate]

This question already has answers here:
2D array of lists in python
(6 answers)
Closed 6 years ago.
I have a list 2D
a = {}
a[0,0] = 1
a[0,1] = 2
a[1,0] = 3
a[1,1] = 4
a[1,2] = 5
...
a[1,n] = 6
Now, I want to access the element a[1,x] (x = is 0 to n)
How can I do?
You'll need a nested loop if I understand what your asking for.
somearray = [[0,1],[2,3],[4,5]]
for row in range(len(somearray)):
for col in range(len(somearray[row])):
print(somearray[row][col], end =" ")
will output:
0 1 2 3 4 5

Python: print without overwriting printed lines

I have
for i in range(0, 11): print i, "\n", i
I'd like my python program to print this way for each for loop
1st loop:
1
1
2nd loop:
1
2
2
1
3rd loop:
1
2
3
3
2
1
I've tried using \r\n or \033[1A but they just overwrite the previous line. Is there a way I can "push" the outputted line down so I don't overwrite it?
One way to do this,
def foo(x, limit):
if x < limit :
print x
foo(x + 1, limit)
print x
foo(1, 11)
It's not possible to do it in 1 for loop as you're currently trying.
As I suggested, you can do it like this by using lists
>>> l1 = []
>>> l2 = []
>>> for i in range(0, 11):
... l1.append(i)
... l2 = [i] + l2
>>> l1.extend(l2)
>>> for i in l1:
... print i
0
1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1
0
Concatenation of two list generators.
>>> ladder = [x for x in range(5)] + [x for x in range(5,-1,-1)]
>>> ladder
[0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
>>> for x in ladder:
... print x
...
0
1
2
3
4
5
4
3
2
1
0
>>>
one of ways to solve this
def two_side_print(start, end):
text = [str(i) for i in range(start,end)]
print '\n'.join(text), '\n', '\n'.join(reversed(text))
two_side_print(1, 11)
Another option is to save the printed values in a list and print that list in reverse order in each loop iteration.
My suggestion:
l = []
for i in range(1, 5):
print 'Loop '+str(i) # This is to ease seeing the printing results
for val in l:
print val
print i
l.insert(len(l),i)
for val in reversed(l):
print val
The output for a loop iterating from 0 to 5:
Loop 1
1
1
Loop 2
1
2
2
1
Loop 3
1
2
3
3
2
1
Loop 4
1
2
3
4
4
3
2
1
I hope this is what you are looking for.
You can use a recursive function to help here:
def print_both_ways(start, end):
print(start)
if start != end:
print_both_ways(start+1, end)
print(start)
Usage:
print_both_ways(1,3) # prints 1,2,3,3,2,1 on separate lines

Categories

Resources