# Sequences and their operations in Python for data scientists

Original article was published by Saurav Anand on Artificial Intelligence on Medium

Everything you need comes to you in perfect time, space and sequence :-Louise Hay

# Introduction

A sequence is a succession of the values bound together by a container. This container reflects it’s type. Almost any stream of data present in python is sequence. The items present in the sequence or in the container are accessible by indexing and slicing.Some of the sequences which python supports are lists,string ,dictionary, tuple and Xrange objects. The built in len()function takes any container as argument and returns the no of items present in it.

Some of the most common sequence operations are :-

• Concatenation :- It is the process of joining two sequence together to form another sequence
• Repetition :- If we want to repeat the items present in the sequence then we can use this method of repetition
• Membership testing :- This is done to check whether a particular element is present or not in the sequence
• Indexing :- Each element is associated with an index where the index of the starting element is 0.
• Slicing :- If we want to slice out certain elements from the sequence , then slicing concept is used where we specify the range of the indexes of items which we need to slice out

# Sequence Types

The most common sequence types are mentioned below:

Now we will see all the sequence operations for all these sequence types.

# Lists

Lists is the most commonly and versatile sequence available in python, which can be written as a list of comma separated values (items) which is enclosed between square brackets.Lists are mainly used where you have collection of data that does not need random access. Also in a situation where you want to deal with values which can be changed.

Creation of list

`my_list=[“python”,”Django”,1,2,3,3.14]print(my_list)['python', 'Django', 1, 2, 3, 3.14]`

Mutable

`my_list[0]=12print(my_list)[12, 'Django', 1, 2, 3, 3.14]`

Length of the list

`len(my_list)6`

Indexing

`my_list[2]1`

Slicing

`#Returns the value from index 1 to the endmy_list[1:]['Django', 1, 2, 3, 3.14]my_list[:3] # return values from index upto 3 (not including 3 )['python', 'Django', 1]my_list[1:3] # elements at 1 ,2 not 3['Django', 1]`

Concatenation

`my_list +[“new_item”][12, 'Django', 1, 2, 3, 3.14, 'new_item']my_list= my_list +["add permantelty"]print(my_list)[12, 'Django', 1, 2, 3, 3.14, 'add permantelty']`

Deletion and Appending

`del(my_list[2])print(my_list)[12, 'Django', 2, 3, 3.14, 'add permantelty']#Adds the element at the end of the listmy_list.append(“Append me”)print(my_list)[12, 'Django', 2, 3, 3.14, 'add permantelty', 'Append me']# Add multiple elements at the end of the codemy_list.extend(['g','h'])print(my_list)[12, 'Django', 2, 3, 3.14, 'add permantelty', 'Append me', 'g', 'h']# Returns the element at the particular index and the particular element is removedmy_list.pop(4)3.14print(my_list)[12, 'Django', 2, 3, 'add permantelty', 'Append me']my_list.remove(“Django”)print(my_list)[12, 2, 3, 'add permantelty', 'Append me']`

Sorting and Reversing

`new_list= [1,5,3,8,2,4]new_list.reverse()print(new_list)[4, 2, 8, 3, 5, 1]new_list.sort()new_list[1, 2, 3, 4, 5, 8]`

Insert

`new_list.insert(1,”script”)new_list[1, 'script', 2, 3, 4, 5, 8, 'python', 'Django']`

Multi Indexing

Consider a tuple inside a list

`#Tuples are given as elements as listlist =[(1,2,3),(“Java”,”Python”)]print(list[1][0])Java`

Please note that as we are using tuple inside the list , the overall list becomes immutable

`list[0][1]=6TypeError                                 Traceback (most recent call last)<ipython-input-34-2d07db11c658> in <module>----> 1 list[0][1]=6TypeError: 'tuple' object does not support item assignment`

……………………………………………………………………………………

# Tuples

A tuple is a sequence of immutable python objects. Tuples are sequence just like lists. Tuple are mainly used when you have to complete the task at shorter period of time. Also when we you have to deal with the values which cannot be changed because tuples are immutable.

A tuple is a collection of constant values and the speed of execution of tuple is much faster as compared to other sequence.It is created by keeping the items separated by commas enclosed between small brackets.

Creation of tuple

`my_tup=(2,4,1,”Python”,”Django”)print(my_tup)(2, 4, 1, 'Python', 'Django')`

Built in functions

`len(my_tup)5max(my_tup)6min(my_tup)2my_tup = ("Python","Djagon")print(my_tup)('Python', 'Djagon')max(my_tup)'Python'min(my_tup)'Djagon'`

Sorting and Reference

`my_tup=(3,6,1,7)sorted(my_tup)1, 3, 6, 7]`

Basic tuple operations

`#Repetition print(my_tup*2)(3, 6, 1, 7, 3, 6, 1, 7)print(len(my_tup))8#Membership testingprint("Java" in my_tup)False`

Other operation

`#Updating tupletup1 = (1,3,5,6)tup2 = (2,4,6,8)tup4= tup1 + tup2print(tup4)(1, 3, 5, 6, 2, 4, 6, 8)#Deleting tuple del(tup2)`

Immutable

`tup=(2,7,3,4,5)tup[0]=8TypeError                                 Traceback (most recent call last)<ipython-input-26-a93662db50ba> in <module>----> 1 tup[0]=8TypeError: 'tuple' object does not support item assignment`

Reverse a tuple

`print(tup[::-1])(5, 4, 3, 7, 2)`

Multi Indexing

`# A list inside a tuplemy_tup=([1,2,3],[“Python”,”Django”,”Java”],[5,6,”C#”])print(len(my_tup))print(my_tup[1][2])3Java`

As we are using lists inside the tuple , the overall tuple becomes mutable we can change it’s values

`#Updating tuple my_tup[0][1]=6print(my_tup)([1, 6, 3], ['Python', 'Django', 'Java'], [5, 6, 'C#'])`

Converting tuples to list and vice versa

`my_tup = (1,2,3,”Python”,”Django”,”C#”)lst=list(my_tup)print(lst)my_tup2=tuple(lst)print(my_tup2)[1, 2, 3, 'Python', 'Django', 'C#'](1, 2, 3, 'Python', 'Django', 'C#')`

# Strings

Strings are sequence which contains group of characters enclosed within the double quotes or single quotes. It is most commonly used sequence in python.

Creating a String

`my_str = “Hello World”print(my_str)'Hello World'`

Built in String Method

`#Length of stringlen(my_str)11#Indexing and slicing of String my_str[2:5] # print elements at index 3,4,5 but not 6'llo'my_str[0]'H'#Membership testingprint("l" in my_str)True#Formatting print("Welcome to %s" %("Python Class"))Welcome to Python Classprint("My name is %s and my age is %d"%("Saurabh",108))My name is Saurabh and my age is 108`

String formatting Operators

Some Other String Methods

`my_str = “welcome to Club”#It capitalizes the first letter of the Stringmy_str.capitalize()'Welcome to club'#Counts how many a string occurs within a particular string print(my_str.count(“e”,0,len(my_str)))2max(my_str)'w'min(my_str)''#Replace all the occurrences of old in string with new or most occurrences if max givenmy_str.replace("e","E",1)'wElcome to Club'#Converts the lowercase letters in string to upper casemy_str.upper()'WELCOME TO CLUB'# Same as find but raises an exception if string is not found my_str.index("L")2`

Immutable

`my_str'wElcomE to Club'my_str[0]='E'TypeError                                 Traceback (most recent call last)<ipython-input-18-02644a18c27d> in <module>----> 1 my_str[0]='E'TypeError: 'str' object does not support item assignment`

# Sets

Set is an unordered list collection of unique items. Sets are defined by values separated by comma inside braces {}. Sets can also be created by calling the built in set function:

Sets are used if we want to collect unique strings or integers from a sequence. For example , admin department of the company wants to track down the details of the employees . Many employees have entered the same password and id. As we know , sets support unique values. So we can covert the lists of id and password into set and get only the unique values.

Unions of Sets

Union of two or more sets is a set of all elements from all sets. Union is performed by using | operator

`set1= {“a”,”b”,”c”,”d”,”e”}set2= {“z”,”x”,”y”,”d”,”a”}print(set1 | set2){'b', 'd', 'y', 'e', 'a', 'c', 'z', 'x'}`

Intersection of Sets

Intersection of two sets is a set of elements which is common in both the sets. Intersection is performed using & operator.

`set1= {“a”,”b”,”c”,”d”,”e”}set2= {“z”,”x”,”y”,”d”,”a”}print(set1 & set2)`

Difference of Sets

Difference of A and B (A-B) is a set of elements that are only in A but not in B. Similarly, B-A is a list of elements in B but not in A.

`set1= {“a”,”b”,”c”,”d”,”e”}set2= {“z”,”x”,”y”,”d”,”a”}print(set1 -set2){'b', 'e', 'c'}`

Operations on sets

`set1= {“a”,”b”,”c”,”d”,”e”}print(“d” in set1)Trueset3 = {“a”,”b”,”c”}print(set3.issubset(set1))Trueprint(set1.issuperset(set3))Trueprint(set1.union(set2))print(set1.intersection(set2))print(set1.difference(set2))print(set1.symmetric_difference(set1)){'e', 'y', 'd', 'a', 'x', 'c', 'b', 'z'}{'a', 'd'}{'b', 'e', 'c'}set()`

`set1= {“a”,”b”,”c”,”d”,”e”}#Add elements to the setset1.add(“p”)print(set1){'a', 'b', 'c', 'd', 'e', 'p'}# Remove the element from the setset1.remove("e")print(set1){'a', 'b', 'c', 'd', 'p'}#Remove the element from set if presentset1.discard(3)print(set1){'a', 'b', 'c', 'd', 'e'}#Removes and return an arbitrary elements from the list set1.pop()'e'#Removes all elements from setset1.clear()print(set1)set()`

# Dictionaries

Dictionary is an unordered collection of key-value pairs. It is generally used when we have huge amount of data. Anita is a receptionist and she has to create records for the employees. She creates an excel sheet , where she enters employee name and Aadhar Card no. The employee name is considered as key and Aadhar card no as value in dictionary.

Creating a dictionary

`my_dict = {‘key1’:’value1',’key2':’value2'}print(my_dict){'key1': 'value1', 'key2': 'value2'}# Call values by their keymy_dict['key2']'value2'#Creating Dictionary through assignments# Create a new dictionaryd = {}# Create a new key through assignmentd['animal'] = 'Dog'# Can do this with any objectd['answer'] = 42#Showprint(d){'animal': 'Dog', 'answer': 42}`

Mutable

`A={‘Age’:24,’Name’:’Saurabh’}A[“Age”]=36print(A){'Age': 36, 'Name': 'Saurabh'}#We can also delete the elements del(dict["Age"]){'Name':'Saurabh"}`

Flexible Data type

`#Its important to note that dictionaries are very flexible in the data types they can hold. For example:my_dict = {‘key1’:123,’key2':[12,23,33],’key3':[‘item0’,’item1',’item2']}{'key1': 123, 'key2': [12, 23, 33], 'key3': ['item0', 'item1', 'item2']}`

Accessing Keys and Values

`my_dict[‘key3’]['item0', 'item1', 'item2']my_dict['key3'][0]'item0'my_dict['key3'][0].upper()'ITEM0'# Subtract 123 from the valuemy_dict['key1'] = my_dict['key1'] - 123#Checkmy_dict['key1']0`

Nesting within Dictionary

`## Nesting with Dictionariesd = {‘key1’:{‘nestkey’:{‘subnestkey’:’value’}}}# Keep calling the keysd[‘key1’][‘nestkey’][‘subnestkey’]'value'`

Built-in functions

`#returns the length of dictionarymy_dict={‘Age’:24,’Name’:’Saurabh’}print(len(my_dict))2#returns the dictionary as stringprint(str(my_dict)){'Age':24,'Name':'Saurabh'}#returns the typeprint(type(my_dict))#returns the value of the key passedprint(my_dict.get('Age')) 24# returns items in the dictionary in the form of tupleprint(my_dict.items())dict_items([(‘Age’,24),(’Name’,’Saurabh’)])#returns keys in dictionariesprint(my_dict.keys())dict_keys([‘Age’,’Name’])#returns values in dictionaries print(my_dict.values())dict_values([24,'Saurabh'])# Sets the dict[key]=default if key is not already in dictionaryprint(my_dict.setdefault(1,4))#Creates copy of the dictionary print(my_dict.copy()){'Age':24,'Name':'Saurabh'}#deletes all the items in the dictionary my_dict.clear()`

Sorting keys in loop

`my_dict={3:'Python',1:'Java',2:'Big Data'}#ks contains the list of keys ks=list(dic.keys())print(ks)[3,1,2]sk=sorted(ks)print(sk)[1,2,3]#print sorted keys with their respective values for key in sk:    print(key,'->',dic[keys])1 -> Java2->  Big Data3->  Python`

Tuple and list in dictionary

`#tuple in dictionarymy_dict={1:(1,2,3),2:(3,4,5)}print(dict){1:(1,2,3),2:(3,4,5)}print(dict[1][1])2# list in dictionarymy_dict ={1:["Python","Java"],2:[1,2,3,4]}print(my_dict){1:["Python","Java"],2:[1,2,3,4]}print(my_dict[1][0])Python`

Thanks ,

Saurav Anand