-1

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 Answers2

1

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

Sanjay SS
  • 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
0
  1. 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']
    
  2. 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'}
  1. 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
  1. 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
Sanket Singh
  • 1,246
  • 1
  • 9
  • 26