Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 940255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:55:30+00:00 2026-05-15T21:55:30+00:00

In Python, a dictionary can be constructed from an iterable collection of tuples: >>>

  • 0

In Python, a dictionary can be constructed from an iterable collection of tuples:

>>> listOfTuples = zip(range(10), [-x for x in range(10)])
>>> listOfTuples
[(0, 0), (1, -1), (2, -2), (3, -3), (4, -4), (5, -5), (6, -6), (7, -7), (8, -8), (9, -9)]
>>> theDict = dict(listOfTuples)
>>> theDict
{0: 0, 1: -1, 2: -2, 3: -3, 4: -4, 5: -5, 6: -6, 7: -7, 8: -8, 9: -9}
>>> 

Is there an equivalent Scala syntax? I see that you can use a varargs type amount of Tuple2s to construct a map, e.g.

scala> val theMap = Map((0,0),(1,-1))
theMap: scala.collection.immutable.Map[Int,Int] = Map((0,0), (1,-1))

scala> theMap(0)
res4: Int = 0

scala> theMap(1)
res5: Int = -1

scala> val tuplePairs = List((0,0),(1,-1))
tuplePairs: List[(Int, Int)] = List((0,0), (1,-1))

scala> val mapFromIterable = Map(tuplePairs)
<console>:6: error: type mismatch;
 found   : List[(Int, Int)]
 required: (?, ?)
       val mapFromIterable = Map(tuplePairs)
                                 ^

I could loop through and assign each value manually, but it seems like there must be a better way.

scala> var theMap:scala.collection.mutable.Map[Int,Int] = scala.collection.mutable.Map()   
theMap: scala.collection.mutable.Map[Int,Int] = Map()

scala> tuplePairs.foreach(x => theMap(x._1) = x._2)                                     

scala> theMap
res13: scala.collection.mutable.Map[Int,Int] = Map((1,-1), (0,0))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:55:31+00:00Added an answer on May 15, 2026 at 9:55 pm

    Using Scala 2.8.0 final, you can do it like this:

    
    scala> val tuplePairs = List((0,0),(1,-1))
    tuplePairs: List[(Int, Int)] = List((0,0), (1,-1))
    
    scala> tuplePairs.toMap
    res0: scala.collection.immutable.Map[Int,Int] = Map((0,0), (1,-1))
    

    If you’re using Scala 2.7.7 you could do something like this, as an alternative to the method you used:

    
    scala> val tuplePairs = List((0,0),(1,-1))
    tuplePairs: List[(Int, Int)] = List((0,0), (1,-1))
    
    scala> Map(tuplePairs: _*)
    res2: scala.collection.immutable.Map[Int,Int] = Map(0 -> 0, 1 -> -1)
    

    but as you can see, in 2.8.0 things have been much improved upon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A regular expression that takes care of removing p attributes… May 16, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer Encapsulate what varies and Prefer object composition to inheritance, are… May 16, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer Short answer: no. Liskov substitution and the nature of public… May 16, 2026 at 5:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Some stated that python dictionary is thread safe. Does it mean I can or
Is there a .net equivalent for urllib I used in Python? I've seen WebRequest
I've found a strange issue with subclassing and dictionary updates in new-style classes: Python
I have such files to parse (from scrapping) with Python: some HTML and JS
How do I create a dictionary in python that is passed to a function
What's the fast method to compress Python objects (list, dictionary, string, etc) before saving
I'm looking for a go language capability similar to the dictionary in python to
I am building a class which subclasses dict , and overrides __setitem__ . I
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a plugin written entirely in Python using PyObjC whose core classes I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.