+ 3
What is the difference between dictionaries, lists and tuples ?
2 odpowiedzi
+ 3
And here's some python-specific stuff:
A List can store any number of types, although you probably shouldn't do it (because that's what tuples are for). [1, "Hello", []] is ok, but you usually use a list to loop over it; if every element is different then you can't really do that. It's bad practice anyway.
So the main difference is that a List is "mutable" (you can change it, add stuff, remove stuff, etc), and a Tuple is "immutable" (if you want to change it, you have to make a new one).
In Dictionaries you can use any immutable thing as key and you can mix types.
{(1,2,3) : "Hello", 4 : "World"} works, although you probably shouldn't do that either.
+ 2
Here's the non-python-specific answer:
A List is an arbitrarily-sized collection with values of the same type. For example, only numbers or only strings, like [1, 2, 3, 4].
A Tuple is a fixed-size collection with values of different types. For example, a 2-tuple with a number and a string, like (1, "Hello").
A Dictionary is an arbitrarily-sized collection that associates one type (the key type) with another (the value type). For example, the dictionary {"size" : 20, "weight" : 10} allows you to put in a string and get out a number. In a lot of languages, only strings are allowed as keys.