Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are differences between List, Dictionary and Tuple in Python? [duplicate]

Tags:

python

What is the difference between list, dictionary and tuple in Python exactly?

like image 403
Ibrahim Nattaj Avatar asked Sep 06 '10 07:09

Ibrahim Nattaj


People also ask

What is the difference between list tuples and dictionary in Python?

List and tuple is an ordered collection of items. Dictionary is unordered collection. List and dictionary objects are mutable i.e. it is possible to add new item or delete and item from it. Tuple is an immutable object.

Does list and tuple allow duplicates in Python?

Tuples allow duplicate members and are indexed. Lists Lists hold a collection of objects that are ordered and mutable (changeable), they are indexed and allow duplicate members. Sets Sets are a collection that is unordered and unindexed. They are mutable (changeable) but do not allow duplicate values to be held.

Can a tuple have duplicates?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

Does dictionary allow duplicates in Python?

The straight answer is NO. You can not have duplicate keys in a dictionary in Python.


1 Answers

A list can store a sequence of objects in a certain order such that you can index into the list, or iterate over the list. List is a mutable type meaning that lists can be modified after they have been created.

A tuple is similar to a list except it is immutable. There is also a semantic difference between a list and a tuple. To quote Nikow's answer:

Tuples have structure, lists have order.

A dictionary is a key-value store. It is not ordered and it requires that the keys are hashable. It is fast for lookups by key.

like image 159
Mark Byers Avatar answered Sep 21 '22 10:09

Mark Byers