Given two lists of string elements list1 and list2:

list1 = {"stel", "segw", "ma7", "str1"};
list2 = {
   "agr: Agriculture, hunting, forestry, fishing", 
   "min: Mining and quarrying", "ma1: Food, beverages, tobacco", 
   "ma2: Textiles, textile products", 
   "ma3: Leather, leather products, footwear", 
   "ma4: Wood & products of wood and cork", 
   "ma5: Pulp, paper, paper products, printing, publishing", 
   "ma6: Coke, refined petroleum, nuclear fuel", 
   "ma7: Chemicals & chemical products", 
   "ma8: Rubber-plastics products", 
   "ma9: Other non-metallic mineral products", 
   "ma10: Manufacture of basic metals", 
   "ma11: Machinery-equipment n.e.c", 
   "ma12: Electrical equipment", 
   "ma13: Transport equipment", 
   "ma14: Manufacturing, nec; recycling",
   "segw: Electricity, gas, water supply",
   "scst: Construction", 
   "ma15: Sale-maintenance; repair-install. of mach. equip.", 
   "swhl: Wholesale trade, except motor vehicles", 
   "srtl: Retail trade, except of motor vehicles", 
   "shot: Hotels & restaurants", "str1: Inland transport", 
   "str2: Water transport", 
   "str3: Air transport", 
   "str4: Other supporting-transport act. of travel agencies", 
   "stel: Post & telecommunications", 
   "sfin: Financial intermediation", 
   "sest: Real estate activities", 
   "sbus: Renting of M&Eq and other business activities", 
   "sadm: Public adm.-defence;compulsory social security", 
   "sedu: Education", 
   "shlt: Human health-social work", 
   "soth: Other community, social & personal services"

I want to use list1 as the key to select the names after : in list2. The selection should be in the order of list1 as follows:

answer = {
  "Post & telecommunications",
  "Electricity, gas, water supply", 
  "Chemicals & chemical products",
  "Inland transport"

7 Answers 7


Use StringSplit to split values in list2, then use KeyTake to extract the indices you want.

Values@KeyTake[(Rule @@@ (StringSplit[#, ": "] & /@ list2)), list1]

(* { Post & telecommunications, 
     Electricity, gas, water supply, 
     Chemicals & chemical products, 
     Inland transport}  *)
  • 2
    $\begingroup$ Seems the most intuitive for me. $\endgroup$ Commented Sep 26, 2023 at 12:07
Lookup[list1] @ MapApply[Rule] @ StringSplit[list2, ":"]

{" Post & telecommunications", " Electricity, gas, water supply", "
Chemicals & chemical products", " Inland transport"}

  • $\begingroup$ very easy to understand (for me) and remember... $\endgroup$ Commented Sep 26, 2023 at 12:52
StringCases[StringJoin[Riffle[list2, "\n"]], 
   RegularExpression[# <> ": (.+)"] -> "$1"] & /@ list1

{{"Post & telecommunications"}, {"Electricity, gas, water supply"},
{"Chemicals & chemical products"}, {"Inland transport"}}


My attempt using Select and StringStartsQ:

getDescriptions[code_List, descriptions_List] := 
order = PositionIndex[code];
StringSplit[#, ": "][[2]] & /@ 
SortBy[Select[descriptions, StringStartsQ[#, Alternatives @@ code] &], 
order[First[StringSplit[#, ":"]]] &]]

Testing getDescriptions:

getDescriptions[list1, list2] === answer


A default clause is provided with the Lookup command, should you want to exploit it. Assuming:

list1 = {"stel", "segw", "ma7", "str1", "xyz"};
lut = Rule @@@ StringSplit[list2, ":"];
Lookup[lut, #, Framed@StringJoin[#, ":", "Not found"]] & /@ list1

enter image description here

(*Make a rule list*)
ruleList = Rule @@@ (StringSplit[#, ":"] & /@ list2);

(*Apply rules to list1*)
answer = list1 /. ruleList;

(*Remove leading and trailing white spaces*)
answer = StringTrim /@ answer;

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.