I have been trying to understand what the difference is between lists, tuples, dictionaries, and sets. All I've been able to gather is they use different brackets or braces. () {} []. Some are able to be changed after the fact and some are not. Please explain in the most simple terms, what the differences are and how you would use them in practical terms.
-
Oh, thats strange, when I went to the search bar and looked for "sets, tuples" i didnt see that search result. Thank you! If i'd seen that post I wouldn't have made this one. – coffeedrinkerman Jul 03 '20 at 05:32
-
no issue, you can use google/duckduckgo with the question you have and add stackoverflow at begining or at end you will get good result related to your problem – sahasrara62 Jul 03 '20 at 05:34
2 Answers
List and tuple are similar to dynamic arrays where in list are mutable but tuples are not. You could checkout the documentation for list and tuple https://docs.python.org/3.8/library/stdtypes.html#sequence-types-list-tuple-range
A set is an unordered collection of objects. Link to the docs: https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset
A dict maps hashable values to arbitrary objects. Mappings are mutable objects. Link to the docs: https://docs.python.org/3.8/library/stdtypes.html#mapping-types-dict

- 568
- 4
- 14
-
Thank you for these links, I'm still trying to understand the lingo, so seeing people explain it using different words really helps to solidify the meanings. – coffeedrinkerman Jul 03 '20 at 05:31
List : Used to store the items. Duplicates can also be stored. Order of storing will be maintained as is. It is mutable which means the same object can be altered after creation. Example :
list1 = ['one',2,3.4,'hello'] print(list1) print(list1[3]) list1[1]=2 #allowed print(list1)
Answer:
'one', 2, 3.4, 'hello'] hello [2, 2, 3.4, 'hello']
Set : Just like list. Only difference is it cannot store the duplicate values. Also there will be no order in which values are stored.
Example:
>>>#set of integers
>>>s = {1, 2, 3}
>>>print(s)
{1, 2, 3}
>>>#print type of s
>>>print(type(s))
<class 'set'>
>>> x = set(['foo', 'bar', 'baz', 'foo', 'qux'])
>>> x
{'qux', 'foo', 'bar', 'baz'}
- Dictionary : Dictionary is basically a storage type in which you can store a value corresponding to a key just like a real life dictionary.
Example:
>>>dictval = {"Nishant":1,"Akash":2,"Ravi":3,"Hari":4}
>>>print(dictval)
{'Nishant': 1, 'Akash': 2, 'Ravi': 3, 'Hari': 4}
>>>print(dictval["Akash"])
2
- Tuple: A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers. The important difference is that tuples are immutable.That we can’t change the elements of tuple once it is assigned whereas in the list, elements can be changed.
Example:
>>>#tuple with mixed datatypes
>>>t = (1, 'raju', 28, 'abc')
>>>print(t)
>>>print(t[1])
(1, 'raju', 28, 'abc')
raju
>>>t = (1, 'raju', 28, 'abc')
>>>print(t)
>>>print(t[3])
>>>t[3] = 'def'
(1, 'raju', 28, 'abc')
abc
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-254e32ad7a3c> in <module>()
2 print(t)
3 print(t[3])
----> 4 t[3] = 'def'
TypeError: 'tuple' object does not support item assignment

- 1,246
- 1
- 9
- 26
-
Thanks! this makes more sense. To be clear I did research examples and definitions but nothing clicked in my head. I just needed to see it phrased in a different way. – coffeedrinkerman Jul 03 '20 at 05:26
-
Haha sure. I think this might helped you. If yes you can upvote. Have a good day. :) – Sanket Singh Jul 03 '20 at 05:29
-
Upvoted! says I don't have enough reputation points quite yet for it to display. Will make sure this gets displayed as soon as I can. – coffeedrinkerman Jul 03 '20 at 05:33
-
Oh ok ok no problem. Welcome to StackOverflow. Keep posting questions. @coffeedrinkerman – Sanket Singh Jul 03 '20 at 05:35