Yes that is possible in Python 3, just use * before the variable like:
print(*list)
This will print the list separated by spaces.
(where * is the unpacking operator that turns a list into positional arguments, print(*[1,2,3]) is the same as print(1,2,3), see also What does the star operator mean, in a function call?)
Yes that is possible in Python 3, just use * before the variable like:
print(*list)
This will print the list separated by spaces.
(where * is the unpacking operator that turns a list into positional arguments, print(*[1,2,3]) is the same as print(1,2,3), see also What does the star operator mean, in a function call?)
You want to say
for i in array:
print(i, end=" ")
The syntax i in array iterates over each member of the list. So, array[i] was trying to access array[1], array[2], and array[3], but the last of these is out of bounds (array has indices 0, 1, and 2).
You can get the same effect with print(" ".join(map(str,array))).
How can I format a list to print each element on a separate line in python? - Stack Overflow
python - How to print list value in same line? - Stack Overflow
Printing list elements on separate lines in Python - Stack Overflow
how can I print different items of a list in multiple lines?
Embrace the future! Just to be complete, you can also do this the Python 3k way by using the print function:
from __future__ import print_function # Py 2.6+; In Py 3k not needed
mylist = ['10', 12, '14'] # Note that 12 is an int
print(*mylist,sep='\n')
Prints:
10
12
14
Eventually, print as Python statement will go away... Might as well start to get used to it.
Use str.join:
In [27]: mylist = ['10', '12', '14']
In [28]: print '\n'.join(mylist)
10
12
14
Try join() to convert your list to string -
" ".join(map(str, li))
Here your list is a list of integers and join() joins every string element of an iterable separated by some string (in this case " "). So, you need to convert each integer to a string first. map() can do that. map() takes a function and apply it on each element of an iterable which is passes as the second arguement (here list li). So, map(str, li) will create an iterable where each element is a string representation of every element of list li
So, your line would be-
print("Case {}: {}".format(case_no, " ".join(map(str, li)), sep=' ', end=''))
You can use generator expression for that too -
print("Case {}: {}".format(case_no, " ".join(str(i) for i in li), sep=' ', end=''))
In this case you are using a generator expression (think of it like list comprehension if you don't know). The generator expresssion iterates over every element of li and returns a stringified (don't know if that's the word) version of that element.
Also, if you just want to print the list and not maintain it for other purposes then you can just simplify it a bit -
t = int(input())
for case_no in range(1, t+1):
n = int(input())
print("Case {}: {}".format(case_no, " ".join(str(i) for i in range(1, n+1) if not n % i), sep=' ', end=''))
You can use print to do it:
data = [[1,2,3,4], [2,4,6]]
for idx,d in enumerate(data,1):
print( f"Case {idx}:", *d) # "splat" the list
prints:
Case 1: 1 2 3 4
Case 2: 2 4 6
Using the splat (*) (or unpacking argument lists) operator to provide all elements of your list as parameters to print:
print( *[1,2,3,4]) == print( 1, 2, 3, 4)
I am trying to create a chart in python using lists. I have a nested list (I think)
list = [[1, 2, 3], [4, 5, 6], [7,8,9]]
what I am trying to do is have each list in a different line so I want the output to be
1, 2, 3
4, 5, 6
7, 8, 9
so they are all in different lines and I have now created a 3x3 grid and i can access each number using its index which i think for example if I wanted to access the 5 on the middle of my chart I would do
[1][1]
row 2, position 2
(I used [1] because row 1 would be [0] and row 3 [2] (I believe))
the problem I have is that it prints it all in one single line so I am not able to create a 2d chart.
how would I create the chart using this method (or any other method sugested by you) and how would i acces each position and change its value (so for example if i wanted to access the position where the 5 currently is and turn it to a 7 so i would get
1, 2, 3
4, 7, 6
7, 8, 9
thanks.
I'm making a card game. I'd like to make a function that prints each card out in a single line, with a space between each card. Of course the tricky part here is the list will grow to an unknown length, and I don't want that to be an issue.
Here's what I want the printout to look like:
___ ___ |3 | |9 | | โฃ | | โฃ | |__3| |__9|
I'll paste some code below that shows were my issue is. First, some context-
I'm using the objects Shoe, PlayerHand, and Card. Shoes are made of Cards. Shoe deals cards to PlayerHand.
PlayerHand.card_list and Shoe.shoe_card_list are lists of cards. Each Card stores its value (1-10, J,K,Q,A) in Card.numlet and the suit in Card.suit.
Just trust that the objects work, it's making the card_list_render work that is the issue.
Here's the code:
def card_front(card):
"""Display all the cards in the cards list."""
print(" ___\n|{} |\n| {} |\n|_{}|".format(card.num_let.ljust(2), card.suit, card.num_let.rjust(2, '_')))
def card_list_render(hand):
if len(hand.card_list) == 1:
card_front(hand.card_list[0])
else:
return ' '.join(card_front(c) for c in hand.card_list)
test_shoe = Shoe(1)
test_hand = PlayerHand()
for card in test_shoe.shoe_card_list:
loop_count = 1
test_hand.card_list.append(card)
print(f"This is loop #{loop_count}, there are {len(test_hand.card_list)} card(s) in the hand")
card_list_render(test_hand)
loop_count += 1so when I run that, here is what I get:
his is loop #1, there are 1 card(s) in the hand
___
|5 |
| โฆ |
|__5|
This is loop #1, there are 2 card(s) in the hand
___
|5 |
| โฆ |
|__5|
___
|5 |
| โฅ |
|__5|
Traceback (most recent call last):
File "C:\Users\REDACTEDNAME\PycharmProjects\FINALbj\visuals.py", line 82, in <module>
card_list_render(test_hand)
File "C:\Users\REDACTEDNAME\PycharmProjects\FINALbj\visuals.py", line 64, in card_list_render
return ' '.join(card_front(c) for c in hand.card_list)
TypeError: sequence item 0: expected str instance, NoneType foundI'm not sure what the issue is. So my join won't put them on the same line, and it breaks with that error. Any suggestions?
Try this just using print:
print(*a, sep=' ')
print accepts sep key that will put between printing of each element of an iterable.
so the above code output will be
1 2 3 4
You can use ',' or \n(new line) to print between each charater.
it means:
print(*a, sep='\n')
output will be:
1
2
3
4
If you have to use lambda AT ANY PRICE, then (using Python 3):
a = [1,2,3,4]
list(map(lambda x:print(x),a))
However, if one liner is enough for you keep in mind that such code:
a = [1,2,3,4]
for each in a: print(each)
is legal Python code, producing same result.
Yet another option is converting int to str and joining them:
print('\n'.join(map(str,a)))