Python Forum
joining a variable number of lists in a comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
joining a variable number of lists in a comprehension
#1
i have a function named matchem that, given a string, finds matching names and returns a list (of strings).  i want to call it for every argument in a command (for arg in sys.argv[1:]).  i want to join all these returned lists together as one big list. in some, perhaps many, cases the function will return an empty list.  i want to do it in a comprehension so it is easy to pass to '|'.join().  i am drawing a blank.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Does this fit your need?
import itertools


lists = ["this", "that"], [], [], ["some", "other", "stuff"]
print("|".join(itertools.chain.from_iterable(lists)))
Output:
this|that|some|other|stuff
If not please provide sample input and output.
Reply
#3
ok so i should do print(itertools.chain.from_itrerable([matchem(a) for a in sys.argv[1:]]))?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Joining two jupyter notebooks and getting an error! Led_Zeppelin 1 1,095 Oct-20-2022, 04:28 PM
Last Post: deanhystad
  Give a number for Variable quest 2 1,480 Jan-31-2022, 08:35 AM
Last Post: ibreeden
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,025 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  What is the value after JOINING an empty list? JaneTan 2 5,061 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  [regex] Good way to parse variable number of items? Winfried 4 2,560 May-15-2020, 01:54 PM
Last Post: Winfried
  Lists first item is a number however i cant compare it with an int getting syntax err Sutsro 4 2,359 Apr-22-2020, 10:22 AM
Last Post: Sutsro
  concatenating lists in a comprehension Skaperen 3 2,089 Jan-01-2020, 08:10 PM
Last Post: ichabod801
  Add two number and variable datatype is int8 ift38375 3 2,864 Aug-05-2019, 05:41 PM
Last Post: DeaD_EyE
  Python - joining xmls together invalid token error jan86 0 2,192 Apr-26-2019, 08:52 AM
Last Post: jan86

Forum Jump:

User Panel Messages

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