10
$\begingroup$

I have two lists of strings:

month = {january, february, march}
year = {2013, 2013, 2013}

I want to concatenante these element by element with the final result of the form:

{january 2013, february 2013, march 2013}

The closest I have come is the following:

Map[StringRiffle[#, " "] &, {month, year}]

Which results in:

{"january february march", "2013 2013 2013"}

Which isn't very close...but at least it doesn't result in an error.

I'm sure this is simple, but I have clearly missed something.

Thanks in advance.

$\endgroup$

9 Answers 9

7
$\begingroup$
month = {"january", "february", "march"}
year = {"2013", "2014", "2015"}
MapThread[StringRiffle[{#1, #2}] &, {month, year}]

or

Inner[StringRiffle[{#1, #2}] &, month, year, List]

Result:

{"january 2013", "february 2014", "march 2015"}

$\endgroup$
6
$\begingroup$

What you posted for your data isn't actually strings, so I'll assume that it was supposed to be like this:

month = {"january", "february", "march"};
year = {"2013", "2013", "2013"};

MapThread is a useful function for performing some sort of joining of lists that have the same structure. In general it looks like this:

MapThread[f, {month, year}]
(* {f["january", "2013"], f["february", "2013"], f["march", "2013"]} *)

So, you just need to figure out what f you want. Maybe something like this:

MapThread[#1 <> " " <> #2 &, {month, year}]
(* {"january 2013", "february 2013", "march 2013"} *)
$\endgroup$
6
$\begingroup$
StringRiffle /@ Transpose @ {month, year}

{"january 2013", "february 2013", "march 2013"}

Your "closest" solution would also be fine if you add Transpose:

Map[StringRiffle[#, " "] &, Transpose @ {month, year}]

{"january 2013", "february 2013", "march 2013"}

$\endgroup$
5
$\begingroup$

Grabbing from Syed

month = {"january", "february", "march"}
year = {"2013", "2014", "2015"}

One suggestion

Apply[StringJoin, Transpose /@ Transpose@{month, year}, 1]

A second one

Function[{x, y}, StringJoin[ToString /@ {x, y}], 
  Listable][month, year]

Both return

{"january2013", "february2014", "march2015"}

Not exactly a third one, but fun nonetheless

Function[, {##}, Listable][month, year]

{{"january", "2013"}, {"february", "2014"}, {"march", "2015"}}

$\endgroup$
5
$\begingroup$
month = {"january", "february", "march"}
year = {"2013", "2014", "2015"}
BlockMap[StringRiffle,Riffle[month,year],2]

(* {"january 2013", "february 2014", "march 2015"} *)

 MapThread[StringTemplate["`` ``"], {month,year}]

(* {"january 2013", "february 2014", "march 2015"} *)

Just for fun

MapThread[StringTemplate["`2` `1`", 
  CombinerFunction-> Composition[StringJoin,Capitalize]], {year,month}]

(* {"January 2013", "February 2014", "March 2015"} *)
MapThread[StringTemplate["`2` `1`", CombinerFunction -> 
  Composition[Decapitalize@*ToUpperCase,StringJoin]], {year,month}]

(*{"jANUARY 2013", "fEBRUARY 2014", "mARCH 2015"} *)
$\endgroup$
4
$\begingroup$
month = {"january", "february", "march"}; year = {"2013", "2014", "2015"};

9. Thread + DateString

Map[DateString]@Thread[{month, " ", year}]
{"january 2013", "february 2014", "march 2015"}

10. Thread + StringJoin

MapApply[StringJoin]@Thread[{month, " ", year}]
{"january 2013", "february 2014", "march 2015"} 
Activate @ Thread[Inactive[StringJoin][month, " ", year]]
{"january 2013", "february 2014", "march 2015"}
ReleaseHold @ Thread[Hold[StringJoin][month, " ", year]]
{"january 2013", "february 2014", "march 2015"}
$\endgroup$
2
  • 2
    $\begingroup$ Now we should have more than 10!!! Kudos to everyone :-) $\endgroup$
    – bmf
    Commented Nov 21, 2023 at 7:00
  • $\begingroup$ Many to choose from. Thanks for the help. $\endgroup$
    – Todd Dixon
    Commented Nov 21, 2023 at 12:13
4
$\begingroup$

11. <>

month = {"january", "february", "march"};year = {"2013", "2014", "2015"}

Table[month[[i]]<>" "<>year[[i]],{i,1,Length [month]}]
  • {january 2013, february 2014, march 2015} *

Or

month[[#]]<>" "<>year[[#]]&/@Range[Length[month]]
  • {january 2013, february 2014, march 2015} *

Or

StringRiffle[{mounth[[#]],year[[#]]}&/@Range[Length[month]]
  • {january 2013, february 2014, march 2015} *

For responding exactly to your question, we must make year to string before:

year={2013,2014,2015};year=ToString[year];
$\endgroup$
3
$\begingroup$

Using ReplaceAll:

Transpose[{month, year}] /. x : {_, _} :> Capitalize@StringRiffle[ToString /@ x]

(* {"January 2013", "February 2013", "March 2013"} *)

Or using Cases:

Cases[Transpose[{month, year}], x_ :> Capitalize@StringRiffle[ToString /@ x]]

(* {"January 2013", "February 2013", "March 2013"} *)
$\endgroup$
1
$\begingroup$
month = {"january", "february", "march"};

year = {"2013", "2014", "2015"};

A hash-free variant of lericr's answer

MapThread[StringRiffle @* List, {month, year}]

Using MapApply (new in 13.1)

MapApply[#1 <> " " <> #2 &] @ Transpose[{month, year}]

Using Query

Query[All, StringRiffle] @ Transpose[{month, year}]

All return

{"january 2013", "february 2014", "march 2015"}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.