Python Forum
Taking brackets out of list in print statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Taking brackets out of list in print statement
#1
How do I take brackets out of multiple lists in a print statement. Say I have the following lists of numbers:

j = [2,5]
k = [4,6]
e = [7,8]
r = [9,1]
How would I print "The numbers you have selected are 2,5,4,6,7,8,9,1"? I want to print the numbers from all the lists without the brackets in between.
Reply
#2
An asterisk before a container unpacks the container. *[a, b, c] becomes a, b, c
Reply
#3
Couple of methods:

import itertools
j = [2,5]
k = [4,6]
e = [7,8]
r = [9,1]

#Nested list comprehension
items = [str(x) for sublist in [j, k, e, r] for x in sublist]
print(f"The numbers you have selected are {','.join(items)}")

#chain iterable
items2 = ",".join(map(str,itertools.chain(j,k,e,r)))
print(f"The numbers you have selected are {items2}")
Output:
The numbers you have selected are 2,5,4,6,7,8,9,1 The numbers you have selected are 2,5,4,6,7,8,9,1
Reply
#4
Another way without any datatype conversion:

j = [2,5] 
k = [4,6] 
e = [7,8] 
r = [9,1]

print('The numbers you have selected are', end=' ')
print(*j, *k, *e, *r, sep=', ')
If one will run this file output will be:

Output:
The numbers you have selected are 2, 5, 4, 6, 7, 8, 9, 1
If order is not important and you want things more 'human-readable' then you can sort them as well:

print('The numbers you have selected are', end=' ')
print(*sorted([*j, *k, *e, *r]), sep=', ')
This will output:

Output:
The numbers you have selected are 1, 2, 4, 5, 6, 7, 8, 9
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you get Python to print just one value in a list? 357mag 3 968 May-17-2023, 09:52 PM
Last Post: rob101
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,452 Sep-03-2022, 11:21 PM
Last Post: Winfried
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,602 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Print List to Terminal DaveG 2 1,408 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Reading list items without brackets and quotes jesse68 6 4,523 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Data pulled from SQL comes in brackets nickzsche 3 2,549 Jan-04-2022, 03:39 PM
Last Post: ibreeden
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,169 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  Change a list to integer so I can use IF statement buckssg 3 2,196 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  How to invoke a function with return statement in list comprehension? maiya 4 2,752 Jul-17-2021, 04:30 PM
Last Post: maiya
  An IF statement with a List variable dedesssse 3 7,954 Jul-08-2021, 05:58 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020