python list and Tuple

A simple definition of Data Structure is a particular way of collecting data on a computer. This article is all about Python List()and tuple. They are the most popular data structure in python. We can’t store multiple data in one variable. List and tuple are a collection of data. So we can store multiple data in a single variable with help of list and tuple. Firstly we are learning about python list after that tuple. Here is a link for python course from basic to advance.

python list()

The list is a kind of collection that allows putting many values in a single variable. A list is a very helpful data structure because we can carry all many values in one continent package. In list, the continent is surrounded by square bracket and element in the list are surround by a comma. A list element can be any python object. List are mutable so we can change the element of a list using index operation. Let’s discuss different list operation here.

Looking Inside python list()

Like String, we can look inside list using an index value. The index value is range from 0 to n-1. We can get any single element in the list using index value inside the square bracket. You get index error if you attempt to index beyond the end of the element. You can also apply a range of index for slice list. Following code illustrate this description in python code.

list1=[12,30,21,31,24]
print(list1[2])
print(list1[2:5])
output: 
21
[21, 31, 24]
python list()
Index out of bound

Concatenate Two list

we can easily concatenate two lists by using + easily. If we concatenate the first list with a second. The element of a first list is appending at the first element. This is also possible using for loop and append function. Both processes are shown in the following code.

list1=[12,30,21,31,24]
list2=[50,60,70]
print(list1+list2)
list1=[12,30,21,31,24]
list2=[50,60,70]
for i in list2:
    list1.append(i)
output:
[12, 30, 21, 31, 24, 50, 60, 70]

Output are same but list1+list2 return new list. But by using append method list1 is extend with new element.

Python List Method

There are various method are use in Python List. Some method are discuss here.

append()

append method is used to appending element at the list. Element is adding at end of the last element of a list. We can append either element or list by the append method. Following program show python append method.

lst=['ram','hari']
print(lst)
lst.append('gita')
print(lst)
output:
['ram', 'hari']
['ram', 'hari', 'gita']

Insert()

Insert method is using for adding element in a list. It’s different from append because append adds an element at last but insert method is using to add an element in a specific index. The insert() method take two parameters first one is index and second one is a value which you want to add on the list. Following program show python insert() function.

lst=['ram', 'hari', 'gita']
print(lst)
lst.insert(2,'gopal')
print(lst)
output:
['ram', 'hari', 'gita']
['ram', 'hari', 'gopal', 'gita']

Count()

Count method is using for calculating total occurrence of element in list. Following code show count() method.

lst=['ram', 'hari', 'gopal', 'gita','ram','ram']
print(lst.count('ram'))
output:
3

Index()

The index method is using to find out the index of the element. If the list contains more same element index method return index of the first element. Following code show the Index method in brief.

lst=['ram', 'hari', 'gopal', 'gita','ram','ram']
print(lst.index('ram'))
output:
0

sort()

The element inside a list is sort by using this method. Sort() method return a list having element are sort in some order.

lst=[2,3,5,6,7,3,4]
lst.sort()
print(lst)
output:
[2, 3, 3, 4, 5, 6, 7]

Reverse()

Like sort method it’s using for sorting data in reverse order. This method returns a list of an element are sort in reverse order.

lst=[2,3,5,6,7,3,4]
lst.reverse()
print(lst)
output:
[4, 3, 7, 6, 5, 3, 2]

pop()

The pop method is using to remove an element from a list. If we mention specific index inside pop then it removes that element otherwise it removes the last element from a list.

lst=['ram', 'hari', 'gopal', 'gita','ram','ram']
print(lst)
lst.pop()
print(lst)
lst.pop(3)
print(lst)
output:
['ram', 'hari', 'gopal', 'gita', 'ram', 'ram']
['ram', 'hari', 'gopal', 'gita', 'ram']
['ram', 'hari', 'gopal', 'ram']

List Function

python have some builtin function for list. Here we discuss some list function.

Sum()

this function is used for calculating the total sum of the list element. If the list has mixed data type some function throw type error.

lst=[2,3,5,6,7,3,4]
print(sum(lst))
output:
30
type error

Min()

min function returns a minimum element from the list. If a list contains multiple datatypes then it throws type error same as sum function.

lst=[2,3,5,6,7,3,4]
print(min(lst))
output:
2

Max()

max function is used to find out max element in a list. Likewise min it’s a throw type error if a list has different data type element.

lst=[2,3,5,6,7,3,4]
print(max(lst))
output:
7

len()

len function is using to find the length of the list. Total element in the list is calculated by using the len function. following program show len function in brief.

python len() function

Looping Through list

On python List, we can also apply the python loop. List loop is iterate on every element of the list. For better understanding how does it’s work? let’s make a simple program that counts the occurrence of the element on a given list This is a simple loop that loops through each element in the list and counts the number of time character is encounters. Here is a code for this problem.

lst=[2,3,5,6,7,3,4]
count=0
for i in lst:
    if i==3:
        count =count+1
print(count)
output:
2

List Comprehension

List comprehension is one of the most popular topics in python. it’s used for creating a list in one single line. With a help of comprehension, we are able to create a list in one line along with also apply if-else condition within comprehension. Following code show simple example of list comprehension.

square=[i**2 for i in range(1,11)]
print(square)
output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

List Comprehension With if Statement

we can apply if condition inside a loop and create a list in one single line. Following code show list comprehension with if statement.

number=range(1,11)
evennumber=[i for i in number if i%2==0]
print(evennumber)
output:
[2, 4, 6, 8, 10]

List Comprehension with if-else Statement

Like if statement inside list we can also apply else statement. Following code show list comprehension with the if-else statement.

number=range(1,11)
evennumber=[i*2 if(i%2==0) else -i for i in number]
print(evennumber)
output:
[-1, 4, -3, 8, -5, 12, -7, 16, -9, 20]

Python Tuple

A tuple is another kind of sequence that functions much like a list. Unlike a list, once you create a tuple you cannot alter the content of tuple. Since Python does not have to build tuple structures to be modifiable, they are simpler and more efficient in terms of memory use and performance than lists.

Looking inside tuple

Like String and list element inside tuple are looking using an index value. The index value is range from 0 to n-1. We can get any single element in a tuple using index value inside a square bracket. Seen index error if you attempt to index beyond the end of the element. You can also apply a range of index for slice tuple. Following code illustrate this description in python code.

tuple1=(12,30,21,31,24)
print(tuple1[2])
print(tuple1[2:5])
21
(21, 31, 24)

Tuple and Assessment

we can put a tuple on the left-hand side of an assignment statement. We can even omit the parenthesis following program show tuple assessment in brief.

x,y=('hello','world')
print(x)
print(y)
output:
hello
world

Tuple Comparison

Comparison operation is supported by python tuple. Comparison operation is between every element from first to last. If the first element is equal then python goes on the next element until it finds the difference between element.

tuple1=(2,3,5,6)
tuple2=(2,3,4,7)
tuple1>tuple2
output:
True

Conclusion

OK, this is the end of the article I hope you can get a good lesson what I deliver in this article. I ask forgiveness for any word and behave which are not to be. Thank you for your kind and attention guys. Stay tuned for the next article. if you are searching for free machine learning and data science course here is a link. If you have any question regarding this article please feel free to comment below.

Leave a Reply