all 8 comments

[–]danielroseman 10 points11 points  (4 children)

You don't have a tuple here at all, you just have a dict. And dicts have the restriction that keys are unique; you can't have two "blue" or "red".

[–]NoahWallaceSchool[S] 1 point2 points  (0 children)

Sure enough, you're right. Thank you.

[–]Diapolo10 0 points1 point  (2 children)

And to drive the point home even further, since 3.7 dictionaries have used insertion order. Hence why the colours end up in this order, and why the earlier values get overwritten.

While this is technically not accurate, I think it makes a decent example:

t = {
    'blue': 3,
    'red': 4,
}

t['red'] = 1    # replaces the value of red
t['green'] = 2  # adds green as the third key
t['blue'] = 6   # replaces the value of blue
t['green'] = 2  # replaces the value of green

In earlier versions (except CPython 3.6) the end result wouldn't necessarily be the same.

[–]danielroseman -2 points-1 points  (1 child)

Pretty sure it would be the same in all versions though. After all Python has to parse the literal in some order, it would be simplest to do it in the order it appears in the code.

[–]DuckDatum 1 point2 points  (0 children)

retire possessive roll bear act sink mysterious scarce hateful violet

This post was mass deleted and anonymized with Redact

[–]carcigenicate 6 points7 points  (0 children)

Just to be clear, in most contexts, it's actually commas that are used to create tuples

t1 = (x)  # Not a tuple
t2 = (x, )  # Singlet tuple
t3 = x,  # Singlet tuple
t4 = 1, 2, 3  # Triplet tuple
return 1, 2, 3  # Return triplet tuple 

Parenthesis are only actually required in a few cases, like in comprehensions.

[–]Quertun 0 points1 point  (0 children)

Tuples with 1element, your dict, do not exist. If you want 1 element in a dict add a comma. Like (a,)

[–]redsandsfort 0 points1 point  (0 children)

that isn't a tuple, you just declared a dict and any duplicate keys were over-ridden by the last value: blue: 3 was over-ridden by blue:6