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
  Why doesn't this print statement work? stylingpat 10 784 Mar-23-2021, 07:54 PM
Last Post: buran
  Getting a certain value from inside brackets. LeoT 5 500 Mar-01-2021, 03:34 PM
Last Post: buran
  Print max numbers in a list jimmoriarty 1 456 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Print variable values from a list of variables xnightwingx 3 619 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 656 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 687 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Removing internal brackets from a string Astrikor 4 722 Jun-04-2020, 07:54 PM
Last Post: Astrikor
  taking input doesnt print as list bntayfur 2 598 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  using element on a list as condition statement glennford49 11 1,489 May-21-2020, 12:53 PM
Last Post: hussainmujtaba
  Help: list comprehension for loop with double if statement mart79 3 701 May-04-2020, 06:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

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