1

I have confused with lists, tuples, sets and dictionaries someone give me clear cut idea. Give me the difference from your understanding don't give the text book definitions.

Tintumon M
  • 1,156
  • 2
  • 14
  • 36
abdkumar
  • 142
  • 1
  • 2
  • 10
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Jun 29 '17 at 17:43
  • 1
    just google that and you should get a good answer – Jamal H Jun 29 '17 at 17:44
  • 1
    http://www.google.com/ – victor Jun 29 '17 at 17:44
  • You should probably try to actually understand the "textbook definitions", since they are important. But alternatively, you should read the [docs](https://docs.python.org/3.5/library/stdtypes.html) which are pretty good. – juanpa.arrivillaga Jun 29 '17 at 18:04
  • 1
    Here is a [good tutorial](http://thomas-cokelaer.info/tutorials/python/data_structures.html) on topic. –  Jul 24 '18 at 05:38

1 Answers1

8

A list is a sequence of elements in a specific order. You can access elements with a numerical index, e.g. the_list[3]. The time taken for several operations such as testing if the list contains an element is O(n), i.e. proportional to the length of the list.

A tuple is basically an immutable list, meaning you can't add, remove, or replace any elements.

A set has no order, but has the advantage over a list that testing if the set contains an element is much faster, almost regardless of the size of the set. It also has some handy operations such as union and intersection.

A dictionary is a mapping from keys to values where the keys can be all sorts of different objects, in contrast to lists where the 'keys' can only be numbers. So you can have the_dict = {'abc': 3, 'def': 8} and then the_dict['abc'] is 3. They keys of a dict are much like a set: they have no order and you can test for their existence quickly.

The elements of a set and the keys of a dict must be hashable. Numbers, strings, tuples, and many other things are hashable. Lists, sets, and dicts are not hashable.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • change that sentence ,you can add elements to the tuples but you cant remove and replace an element. – abdkumar Jul 06 '17 at 07:01
  • 2
    @abdkumar no, you can't. When you 'add an element' you're creating a brand new tuple and the original is left unchanged. – Alex Hall Jul 06 '17 at 07:33
  • `frozenset` is also worth mentioning, it is just immutable (and thus hashable) `set`. –  Jul 24 '18 at 05:41
  • 2
    The answer also, omits the fact that **`set`s do not allow repeated values**. –  Jul 25 '18 at 11:42