INTRODUCTION TO LIST

In this tutorial we will be going to learn about list , how to create it, and how can we perform various operations on it .

In the previous tutorial we have learnt about  introduction to strings, how they are created , how can we concatenate two strings , how can we iterate these strings , and learnt about various operations like indexing  , slicing , and  many inbuilt operations that can be performed on the strings .

 

List

It is a datatype which  is as similar as array. The difference between array and list  is that array contains ordered sequence of  items of similar data types, whereas list contains ordered sequence of items that can be of different data types.

Python list is mutable, which means that the value of items can be altered by accessing it using operator [ ] .

How to create and print list

>>>a1=[ ]     #creation

>>>a2=[1,2,3,4,5]

>>>a3=[‘hi’, ‘helo’]

>>>list4=[‘a’,’b’,’c’]

>>>list5=[[1,2],3,[4,5,’j’],’o’]

>>>print(a2)

>>>[1,2,3,4,5]

>>>print(a3)

>>>[‘hi,’helo’]

Concatenation of list

>>>z=a2+a3

>>>print(z)

>>>[1,2,’hi’,’helo’]

How to access elements from list

  • Once we  create it, we need to access the elements  for performing the operations. For this, Python has indexing operator [ ] and  Index starts from 0.

>>>list1=[‘a’,’b’,’c’,’d’,’e’]

>>>print(list1[0])

>>>a

>>>print(list1[-1])

>>>e

  • we can also access elements using slicing ,list[start:stop:step] as done in strings.

>>>a=[1,2,3,4,5]

>>>a[1 :4]

>>>[2,3,4]

How to insert elements to the list

we can insert element using insert function . here insert(position, value)

>>>list1=[1,2,4,3]

>>>print(list1.insert(2,0))

>>>[1,2,0,4,3]

How to add item at the end 

we can add item at the end of the list using :

  • append():it is used to add an element to the end .
  • extend():it is used to add multiple items at the end .

>>>list1=[1,2,3]

>>>list1.append(4)

>>>print(list1)

>>>[1,2,3,4]

>>>list1.extend([5,6])

>>>print(list1)

>>>[1,2,3,4,5,6]

>>>list1.extend(7)

>>>print(list1)

>>>[1,2,3,4,5,6,7]

How to modify elements 

modification of  elements  can be done  using its  index and the assignment operator (=). We can change both single element or some sequence of elements in the list.

>>>list1=[1,2,3,4]

>>>list1[0]=9

>>>print(list1)

>>>[9,1,2,3,4]

>>>list1[2:3]=[6,7]

>>>print(list1)

>>>[9,1,6,7,4]

How to remove items

There are few methods to remove items . They are listed below.

  • list_name.remove(item_name): Removes the item with item_name  .
  • list_name.pop(index): Removes item with provided index and returns it.
  • del list_name[index]: Removes item with given index
  • del list_name: Deletes the liist
  • list_name.clear(): Removes every item from liist_name

>>>list1=[1,2,3,4]

>>>list1.remove(1)

>>>print(list1)

>>>[2,3,4]

>>>list1.pop(2)

>>>print(list1)

>>>[3,4]

>>>del list1(1)

>>>print (list1)

>>>[3]

>>>list1.clear()

>>>print(list1)

>>>[ ]

How to find the number of occurrences of element

To count the total number of times the element occurred , we have an  Python has an inbuilt function called “count ” that is, list_name.count(item-name), which returns the number of times the item appears .

>>>a=[1,2,3,4,1,1,1]

>>>print(a.count(1))

>>>4

How to reverse order of items 

When we want to reverse the order of items , then in python we have an inbuilt function called list-name.reverse(), which reverses the order of appearance of items in the list and return the answer.

>>>print(a.reverse())

>>>[1,1,1,4,3,2,1]

How to find the index position of item

To find the index of particular element we have liist_name.index(element) .This function will return the index of the first appearance of that particular item .

>>>print(a.index(4))

>>>3

 How to find  the maximum  and minimum element 

Python has max() and min()  function to find the maximum and minimum item.

>>>print(max(a))

>>>4

>>>print(min(a))

>>>1

Now some builtin functions 

  • round() :-Rounds off to the given number of digits and returns the floating point number.
  • sort();- returns sorted items in ascending order of sequence.
  • copy():- returns a copy to user.
  • reduce():-apply a particular function passed in its argument to all elements stores the intermediate result and only returns the final summation value.
  • sum();- returns Sum of all the numbers  .
  • ord():-Returns an integer representing the Unicode code point of the given Unicode character.
  • lambda():-This function can have any number of arguments but only one expression, which is evaluated and returned.
  • map():-returns a list of the results after applying the given function to each item of a given iterable.
  • filter():-tests if each element of a list true or not.
  • all():-Returns true if all element are true or if list is empty.
  • any():-return true if any element of the list is true. if list is empty, return false.

http://www.geeksforgeeks.org

https://docs.python.org

%d bloggers like this: