-3

I see it everywhere - in the literature, in the documentation, etc. Why do developers use this construction:

params = (
    ('q', who),
    ('tbm', 'isch')
)

instead of this

params = {
     'q': who,
     'tbm': 'isch',
}

The second one seems to be way easier to use.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ivan Adanenko
  • 445
  • 2
  • 6
  • 18

2 Answers2

2

One of the reason might be that the programmer predicts that there will be identical keys in a tuple, which cannot be done with a dictionary. Also I heard that tuples use less space in memory, because of their lack of functionality compared to lists and dictionaries.

1

One reason is unpacking multiple values from a tuple is easier. Returning a tuple from a function is often done because there is some standard expected pattern to handle the multiple values returned. Imagine if you have some function that divides two numbers and returns the div output but also the remainder. You could return and you call it with 42 and 7. It could return {div_res: 6, rem: 0} or it could return (7, 0) and the expected pattern is div_res, rem = div_and_mod(42, 7).

BWStearns
  • 2,567
  • 2
  • 19
  • 33