Skip to main content
python-collection-example

Python Collection Example

This tutorial help to understand python collection types. There are four collection data types in python. It’s a very common and useful data type for python programming language.

There are some in-built collection types like list, dict, set and tuple etc. Python has a number of modules which are providing additional data structures to store collected data. The most popular is the collection module.

  • List: This is an ordered and changeable collection of data types. It Allows duplicate members.
  • Tuple: This is a collection that is ordered and unchangeable. It Allows duplicate members.
  • Set: This is an unordered and unindexed collection type. No duplicate members allow.
  • Dictionary: This is a collection that is unordered, changeable and indexed. No duplicate members allows.

List

A list is a collection that is ordered and changeable. The Python lists are defined with square brackets.

list_a = ["success", "danger", "running"]
print(list_a)

Tuple

A tuple is a collection that is ordered and unchangeable. The Python tuples are written with round brackets.

tuple_a = ("success", "danger", "running")
print(tuple_a)

Sets

A set is a collection that is unordered and unindexed. You can define sets using curly bracket :

sets_a = {"success", "danger", "running"}
print(sets_a)

Dictionary

A Dictionary is a collection that is unordered, changeable and indexed. You can define a python dictionary using a curly bracket. The Dictionary has key-value pair.

dict_a = {"green" : "success", "red" : "danger", "yellow" : "running"}
print(dict_a)

Leave a Reply

Your email address will not be published. Required fields are marked *