Print 5 elements of a list per line [duplicate] - python

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
"""

Related

How do you format a horizontal list in python? [duplicate]

This question already has answers here:
Printing Lists as Tabular Data
(20 answers)
Closed 7 months ago.
I'm trying to print a formatted horizontal list after a prompt. This is what I've tried:
def displayList(prompt, list_):
print(f"{prompt:>20}:",*list_, sep = f"{'':>4}")
but when displaying multiple lists on top of each other I get this:
List 1: 6 2 4 8 4
List 2: 6 3 5 9 1
Added List: 12 5 9 17 5
Multpilied List: 36 6 20 72 4
but I want this:
List 1: 5 4 8 6 4
List 2: 4 9 10 1 2
Added List: 9 13 18 7 6
Multpilied List: 20 36 80 6 8
You need to apply a field width to each list item rather than a fixed-width separator. For example:
print(f'{prompt:>20}:' + ', '.join(f'{e:>10}' for e in list_))

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

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)

Convert a list of string into a vector [duplicate]

This question already has answers here:
String to list in Python
(5 answers)
Closed 5 years ago.
Given:
['1 -1 1 1 1 1 1 1 1']
How can I convert it (efficiently) to be a vector of integers something like:
[1 -1 1 1 1 1 1 1 1]
Thank you.
return [int(n)
for n in s.split()]

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

print all loops once/together in python [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 8 years ago.
if I have a simple for loop like:
for i in range(10):
total = 0
total = total + i
print(total)
How can I print the loops once/together like:
1 2 3 4 5 6 7 8 9
Instead of every loop by itself:
1
2
3 etc
for i in range(10):
total = 0
total = total + i
print(total,end=" ")
The ability of print function, end of the each line changing from \n to " "

Categories

Resources