Jay Taylor's notes

back to listing index

scala - Convert List of tuple to map (and deal with duplicate key ?) - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: stackoverflow.com
Clipped on: 2012-10-11
 
 
 

I was thinking about a nice way to convert a List of tuple with duplicate key [("a","b"),("c","d"),("a","f")] into map ("a" -> ["b", "f"], "c" -> ["d"]). Normally (in python), I'd create an empty map and for-loop over the list and check for duplicate key. But I am looking for something more scala-ish and clever solution here.

btw, actual type of key-value I use here is (Int, Node) and I want to turn into a map of (Int -> NodeSeq)



 
asked Nov 4 '11 at 23:08
Image (Asset 1/3) alt=
Tg.

725 5 15
 


88% accept rate
  add comment
 
 

2 Answers

 
 
up vote 9 down vote accepted

Group and then project:

scala
]]>
 val x = List("a" -> "b", "c" -> "d", "a" -> "f")

//x: List[(java.lang.String, java.lang.String)] = List((a,b), (c,d), (a,f))

scala
]]> x.groupBy(_._1).map { case (k,v) => (k,v.map(_._2))}

//res1: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] = Map(c -> List(d), a -> List(b, f))

More scalish way to use fold, in the way like there (skip map f step).

answered Nov 4 '11 at 23:25
Image (Asset 2/3) alt=
om-nom-nom

8,448 3 27 52
 
 
  upvote
  flag
Thanks, simple but works. I will look into your link later. – Tg. Nov 4 '11 at 23:50
 
 
add comment
 

Here's another alternative:

x.groupBy(_._1).mapValues(_.map(_._2))



 
answered Nov 4 '11 at 23:57
Image (Asset 3/3) alt=
Daniel C. Sobral

100k 14 175 359
 
 
  upvote
  flag
This gives us a Map[String, SeqView[String,Seq[_]]]... is this intentional? – Luigi Plinge Nov 5 '11 at 1:04
  upvote
  flag
@LuigiPlinge A SeqView[String,Seq[_]] is also a Seq[String]. Still in hindsight I don't think that is worthwhile, so I removed the view. mapValues will do a view anyway on the values. – Daniel C. Sobral Nov 5 '11 at 2:59
 
 
add comment
 

Your Answer

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
 
 
  community wiki
 
 
 
 

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

tagged

 × 11418

 × 4669

asked

11 months ago

viewed

410 times

active

11 months ago

Related

 
about | faq | blog | chat | data | legal | privacy policy | jobs | advertising info | mobile | contact us | feedback
 stackoverflow.com   api/apps   careers   serverfault.com   superuser.com   meta   area 51   webapps   gaming   ubuntu   webmasters   cooking   game development   math   photography   stats   tex   english   theoretical cs   programmers   unix   apple   wordpress   physics   home improvement   gis   electrical engineering   android   security   bicycles   dba   drupal   sharepoint   scifi & fantasy   user experience   skeptics   rpg   judaism   mathematica 
   
rev 2012.10.12.4417
site design / logo © 2012 stack exchange inc; user contributions licensed under cc-wiki with attribution required