Some Facts About Tuples, Lists, Dictionary and Sets

Hello Friends, lets learn some Fun facts about:

Tuples, Lists, String, Dictionary and Sets.

Before that, lets get used to some frequently used terms to define the data types.

Mutable and Immutable Data Structures:

Mutable are those which can been modified that means the values to which changes can be made.

Immutable are those which once assigned , the values cannot be changed.

Heterogeneous : This is the type that accepts different data types. There is no necessity for all the data to be of a single type.

TUPLE

A Tuple is an ordered sequence of items enclosed in ROUND BRACKETS and Separated Commas.

List can be used in a Tuple.

Tuple can be used in a Tuple.

For example: tup=(22,’Sarah’,456, ‘Milly’,[23,’farah’,781,’Silly’])

Here, [23,’farah’,781,’Silly’] is a list, similarly tuple can also be used while assigning a tuple.

Immutable: This means that I cannot change the Values of ‘Sarah’ or 22 to any other string or number. If I have to, then it will have to be a New Tuple, all together.

Heterogeneous.

LIST

A List is an ordered sequence of items enclosed in SQUARE BRACKETS and Separated by Commas.

List can be used in List.

Tuple can be used in List.

For example: lis1=[1,5.6,True,(4–7j),’Hello’,[1,2],[3.9,’World’],(4,6,’Jija’)]

Heterogeneous: Now, as we can see that even complex no.’s can be a part of a list.

Mutable: This means that any value can be changed, datatype too can be changed. Suppose if I want to change ‘Hello’ with number 143, it will accept the change.

STRINGS

Immutable: Because indexed value cannot be changed.

For example, when I assign , name=’Shaira’. Now, I can’t make changes to any single value of the string ‘Shaira’ , for example, I cannot replace ‘K’ in place of ‘Sh’ and make it ‘Kaira’.

There are two types of indexing: Positive Indexing and Negative Indexing

Positive Indexing : name=’Aravind’

Now, Input: name[2]

Output: ‘a’

Because indexing is done from ‘0’ till ’n’ places, So here,

‘A’ is at 0

‘r’ is at 1

‘a’ is at 2

‘v’ is at 3 and so on.

Negative Indexing: name=’Aravind’

In above example, ‘d’ is at position 6.

but, it can also be written as name[-1]. Similarly, if I want to derive ’n’, I will type name[5] or name[-2] in Negative Indexing.

DICTIONARY

Under this data type, the concept of Keys and Values is used.

Dictionary is assigned in BRACES and Separated by Commas.

For example: To assign a dictionary to variable name ‘dict’, this is the way to assign it.

dict={‘key’:value, ‘key’:value, ‘key’:value}

For example: dict={‘country’: ’India’, ‘state’: ‘U.P.’, ‘age’:64}

Mutable.

Heterogeneous.

In Dictionary, A list can be assigned as a Value to a Key.

For example: emp={‘Name’:[‘Raju’,’Raghav’,’Ram’], ‘Age’:[27,25,26], ’Designation’: [‘DC’,’DS’,’JDS’]}

Now to retrieve the Values of KEY ‘Name’, I can give the command:

emp [‘Name’] Note: Name is in SQUARE Brackets.

Output: [‘Raju’,’Raghav’,’Ram’]

We can also use get() function, to retrieve the Value of any Key, but with a slightly different SYNTAX, as below:

emp.get(‘country’) Note: the .get() , also the ROUND BRACKETS.

So, ROUND BRACKETS are used to retrieve value of a key, if we are using get() function.

Output: ‘India’

SETS

Sets are a group of UNIQUE VALUES, which means if same value is repeated in a set. In the Output, it will display that Value only for ONCE.

Indexing cannot be performed in Sets.

Mutable.

Heterogeneous.

Sets can be built using set([]) with ROUND and SQUARE Brackets and both are must

OR just the BRACES {} . :)

For example:

Input: myset1=set([‘Data Science’, ‘AI’, ‘Machine Learning’, ‘Statistics’])

Output: {‘Data Science’, ‘AI’, ‘Machine Learning’,’Statistics’}

Remember: A SET, in the Output , will always be given in BRACES, always. No matter, what SYNTAX you are giving.

Input: myset1={1,1,1,2,2,5,10,5,80,80,55,55,60,True,False,0} (only braces)

Output: {1,2,5,10,80,55,60,False}

Note:

  1. Unique Values are taken in the output, values are not repeated.

Concluding the topic, I would say, really minor differences are there, that, if kept in mind, can help understand the exact usage of each one of these data structures.

Thanks & Regards

Vinita