0

What is the difference between list,tuple,dictionaries in real time.I don't need abstract answer.How do I know which one to use?

list,tuple,

8th Jun 2017, 7:34 PM
phani indra
phani indra - avatar
6 Antworten
+ 9
Another cool type of collection is a set. Sets are great because they store only unique elements - that feature can be used for instant deduplication of lists, just convert that into a set and back :)
8th Jun 2017, 8:07 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
All are collection types. Lists are collections of values of any type (like integers, strings, other lists, object, etc.) - they are indexed from 0 to x, where x equals the list's length minus 1. Lists are mutable, sliceable, expandable and replaceable. Tuples are very similar to lists with an exception that they are immutable. Once created, a tuple cannot be changed (a very important feature which may be beneficial rather than disadvantageous). Dictionaries are collections of key-value pairs, where keys are unique and has to be of an immutable type (it can be an integer, a string or a tuple for example). Dictionaries themselves are mutable (changeable), expandable and you can call their indexes. But, they are not ordered, which means you can't be sure of an order of iteration, for example.
8th Jun 2017, 7:49 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
@Abhishek Dubey yes
8th Jun 2017, 8:02 PM
Aibek T.
Aibek T. - avatar
+ 3
List is a mutable collection. It is more suitable when you want to make some modifications on your data dynamicaly. Tuples are immutable, so their values do not get changed. Dictionaries are used to store data in key:value pairs. So you can easily access some value by it's key in your code.
8th Jun 2017, 7:41 PM
Aibek T.
Aibek T. - avatar
+ 1
Can we change the values associated with keys in dictionary?
8th Jun 2017, 8:00 PM
Abhishek Dubey
Abhishek Dubey - avatar
+ 1
@Abhishek - yes, you can change the values: mydict = {'a':1, 'b':2} print (mydict['a']) mydict['a']='changed' print (mydict['a']) Changing the keys is another matter, however - you cannot change a key, but you can delete it and create another key (which leads pretty much to the same end result :) )
9th Jun 2017, 7:35 AM
Bogdan Sass
Bogdan Sass - avatar