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 .
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
Concatenation of list
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.
- we can also access elements using slicing ,list[start:stop:step] as done in strings.
How to insert elements to the list
we can insert element using insert function . here insert(position, value)
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 .
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.
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
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 .
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.
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 .
How to find the maximum and minimum element
Python has max() and min() function to find the maximum and minimum item.
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.