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
  Print max numbers in a list jimmoriarty 1 197 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Print variable values from a list of variables xnightwingx 3 333 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 306 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 422 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Removing internal brackets from a string Astrikor 4 403 Jun-04-2020, 07:54 PM
Last Post: Astrikor
  taking input doesnt print as list bntayfur 2 354 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  using element on a list as condition statement glennford49 11 823 May-21-2020, 12:53 PM
Last Post: hussainmujtaba
  Help: list comprehension for loop with double if statement mart79 3 411 May-04-2020, 06:34 AM
Last Post: buran
  Trying to print corresponding elements in two different list Darthmarvolo 5 554 Mar-03-2020, 04:29 AM
Last Post: perfringo
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 528 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77

Forum Jump:

User Panel Messages

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