Skip to main content

Learning Python: Lists And Tuples.

Published 4 min readMarkdown
Learning Python: Lists And Tuples.

LIST

What is list?

List is a collection of data enclosed by []. Lists are mutable , which means the values inside the list can be changed anytime . List may contain string , integers ,a list inside a list or all of these in a single list itself.

A list can be created by simply putting values enclosed by [].

List operations.

1. Length of a list .

To know the length of list we use len() function. for ex.

list1=[1,2,3]
len(list) #this will return 3

2. Indexing in list.

Elements of a list are arranged in a definite sequence ,called indexes . The first element is treated as (0)th index and last element is treated as (n-1)th index . for ex.

list1=[10,20,30]
#index of 10 is 0
#index of 20 is 1 
#index of 30 is 2

3. Slicing in ist.

Slicing is sublist of a list. for ex.

list1=[1,2,3,4,5]
list2=list1[1:3] #this will extract a new list from index 1 to 3 .
print(list2) #output will be [2,3] as 3 gets excluded.

4. Adding and removing elements in a list.

a. We can add an element in a list by using append() fucntion .

Append fuction will add an element to a list at the end .Append will only insert 1 element ,to add multiple elements we use insert() fuction.

list1=[1,2,3]
list1.append(4)
#this will add 4 at the end of list1  
#new list is [1,2,3,4]

b. insert fuction

list1=[1,2,3]
list1.insert(4,5,6)
#this will add 4,5,6 at the end of list1
#new list is [1,2,3,4,5,6]

c. To merge two lists extend fucion is used. for ex,

list1=[1,2,3]
list2=[4,5,6]
list1.extend(list2)
#this will merge the two lists and return [1,2,3,4,5,6]

d.To remove an element in a list we use pop() fuction.

pop function will remove the indexed element and return it to the user. for ex.

list1=[1,2,3,4]
list1.pop(3) #this will remove the 4th elements i.e 4 from the list as at 3rd index 4 is there.

e. To remove elements we use remove function. remove fuction will remove the element given by the user . for ex.

list1=[2,3,4,5,6]
list1.remove(5)
#this will remove 5 from the list

f. Sorting :- sort() fucntion will arrange the elements of the list in ascending order .

list1=[4,3,2,6,1]
list1.sort()
#this will sort the list and return [1,2,3,4,6]

HERE IS CODE HAVING MULTIPLE OPERATIONS OF A LIST.

x='hello'
list1=[20,12,32,11,65,32]
print(list(x)) #create a list of alphabets 'hello'
print(list1.append(13)) #this will add 13 to the list.
list2=list1[2:5] #slicing
print(list2) #this will create a new list having 3 elements only.
print(list1.sort()) # this will sort the list in ascending order.
print(list1.reverse()) # this will reverse the list.
print(list1.extend(list2)) #this will add all the elements of list2 in list1.
x=list1.pop() # pop fucntion
print(x) #poped item
print(list1.remove(12)) # this will remove 12 from the list.
#end

TUPLES

What is a tuple?

Tuple is collection a data like list . The values stored in a tuple can be of any data type seperated by commas . Tuples are immutable means the values can not be changed like in list.

A tuple can be created by simply adding values enclosed by () seperated by commas .

Tuple operations.

1. creating a tuple.

x='hello'
tuple1=tuple(x)
print(tuple1)
#this will create a tuple for the word hello where each alphabet gets seperated by a comma.

2. Length of tuple .

Len function is used to find the length of a tuple. for ex.

tuple1=(1,2,34,4)
len(tuple1) #this willl return 4 (the length of tuple1.)

3. Indexing in tuple.

Indexing in tuple is same as of list .

tuple1=(1,2,3,4)
print(tuple1[2])
#this will return 3 ,as 3 is in second index.

4. Concatenation in tuples.

Two tuples can be concatenated by simply using + between them. for ex.

tuple1=(1,2,3)
tuple2=(4,5,6)
tuple3=tuple1+tuple2
print(tuple3)#merged tuple.

5. Deleting a tuple.

A tuple can be deleted by using del() function

tuple1=(1,2,3)
del tuple1 # tuple deleted
print(tuple1) #this will give error as tuple1 is already deleted.

6. Maximum and minumum value in a tuple.

By using min and max fuction we can find the minimum and maximum value in a tuple.

for ex.

tuple1=(2,4,5,6,3)
tuple1.max() # this will return 6
tuple2.max() # this will return 2

HERE IS CODE HAVING MULTIPLE OPERATIONS OF A TUPLE.

x='hello'
tuple1=(20,12,32,11,65,32)
print(tuple1(x)) #create a tuple of alphabets 'hello'
tuple2=tuple1[2:5] #slicing
print(tuple2) #this will create a new tuple having 3 elements only.
print(tuple1[0])# this will return value at index 0.
print(tuple1.max())
del tuple1 #this will delete tuple.
#end

DIFFERENCE BETWEEN A LIST AND A TUPLE.

s.noDIFFERENCES
1.Lists are mutable whereas tuples are immutable .So we can use them according to our preferences. We can use tuple for data that need not to be changed later .
2.Operations in tuple is bit faster than lists.
3.Lists provide more fuctions in comparison to tuple.

No comments yet. Be the first to comment.

More from this blog