Blog

20 Jan 2020

Python Collections

Collections in Python

Python Collections: The collection is a term used in Python which actually can refer any of the four things in Python. All these things are also considered as data types in Python as they can contain multiple values together. They are:- 

  1. List
  2. Tuple
  3. Set
  4. Dictionary

Here we are trying to explain all these things. 

What is List?

A-List is a kind of collection that is ordered and changeable. It allows duplicate members. A-List is defined by putting the values separated by, and enclosed by [ ]. Here ordered also means that the items can be referred by mentioning the position.  On the other hand changeable means, we can alter the items whenever required.

So here is an example –

 Mylist=[‘SQL Server’, ’Power BI’,’Linux OS’,’AWS’,’ Hibernate’,’Power Builder’]

My list will be considered as a List consisting of the values SQL Server, Power BI, Linux OS, AWS, Hibernate & Power Builder. So it is containing 6 items and we can individually refer an item by mentioning the position like –

print(Mylist[1]) –

  • it will print Power BI as an output since in the 0-th element SQL Server is stored and in the next position Power, BI is stored. So in the same way if we give a command like –

print(Mylist[1]) – it is going to print the next element which is Linux OS.

Benefits of Using List:

  • We can sort the elements within the List either in ascending or in descending order.
  • Since a List is changeable so we can add any element or remove an element to or from a list.
  • We can join two different lists to form a single list.
  • We can count the no. of elements within a list using an inbuilt function len()

 

What is Tuple?

A tuple is a kind of collection that is ordered and unchangeable. In Python, tuples are written with parenthesis. A tuple is recognized by the symbol of opening & closing parenthesis and the values are separated by a comma.

Example:

Mytuple=(“New Delhi”,”Agra”,”Jaipur”,”Bhopal”,”Kochi”,”Kolkata”,”Patna”)

 

In the above example, Mytuple is a tuple consisting of various city names. Tuples are often chosen when we anticipate that there will be no change within the elements or hardly there will be many changes within the elements.

 

Benefits of Using Tuple:

  • Tuples are more efficient.
  • Tuples are faster than lists.
  • Like list individual elements can be accessed by their position like print(Mytuple[1]) will produce output as Agra.
  • Tuples can be converted into Lists, and vice-versa.

Note: We cannot perform Add, Delete operations on Tuples which we can do with the list. That is why a Tuple is also known as an immutable object.

We can use a range to access multiple values from a list or a tuple – Like –

tuple1=(‘Physics’, ‘Chemistry’,’Statistics’,’Zoology’,’Political Science’,’Accountancy’)

print(tuple1[2:5])

  • Will produce the output as (‘Statistics’, ‘Zoology’, ‘Political Science’)

 

What is a Set?

A set is a form of a collection which is unordered and unindexed. In Python, sets are written with curly brackets. For e.g.

DeptList={‘Admin’,’ HR’,’ Sales’,’Purchase’,’Acct’}

Further, we cannot access items in a set by referring to an index since sets are unordered the items has no index.  But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in the keyword.

To print all the values within a Set we can use the following code snippet –

DeptList={‘Admin’,’ HR’,’ Sales’,’Purchase’,’Acct’}

for x in DeptList:

            print(x)

The above two lines are more than sufficient to print all the individual elements within the DeptList.

Benefits of Using Set:

  • As the name suggests we can perform some common Set operations like union , intersection and difference on Python set objects. Will be shortly published examples of such things.

 

What is a Dictionary?

A Dictionary is an object which can store the elements in a combination of Key & Value. The combination is expressed with the symbol: which separates the Key & the value. However different elements are separated by, itself.  An example of a Dictionary can be as follows-

myDict={101:’Three Idiots’, 109:’Sholay’, 113:’Shaan’,124:’Karz’,115:’Pink’,120:’16 December’}

Here 101, 109, 113, 124, 115 & 120 are the key values whereas Three Idiots, Sholay, Shaan, Karz, Pink & 16 December are all the values against each key.

 

Benefits of using Dictionary:-

  • Dictionaries are Python’s most powerful data collection. Dictionaries allow us to do fast database-like operations in Python.
  • Dictionary Keys can be of any Python data type. Because keys are used for indexing, they should be immutable or not changeable.
  • Dictionary Values can be of any Python data type. Values can be mutable or immutable.

 

Also, See: Learn Python Programming in 7 Days