1
votes

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.

1
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. on topic and how to ask apply here. StackOverflow is not a design, coding, research, or tutorial service. - Prune
just google that and you should get a good answer - Jamal H
You should probably try to actually understand the "textbook definitions", since they are important. But alternatively, you should read the docs which are pretty good. - juanpa.arrivillaga
Here is a good tutorial on topic. - user8554766

1 Answers

7
votes

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.