This program demonstrates use of various built-in List functions and methods
A list is an object that contains multiple data items. Lists are mutable, this means that their contents can be changed during a program’s execution. Lists are dynamic data structures, meaning that items may be added to them or removed from them. We can use indexing, slicing, and various methods to work with lists in a program.
## # Python's program to demonstrate use of built-in List functions and methods. def main(): print('\n########################################################\n') list1 = ['India', 'Germany', 251, 4000, 'Australia', 5478] list2 = [1000, 2000, 3000, 4000, 5000] list3 = ["Yellow", "Blue", "Green", "Red"] # len : This function returns the total length of the list. print("len :\t\t", len(list1)) # max : This function returns the item from list have largest value. # max not works for list1 it will throw TypeError. print("max [integer]:\t", max(list3)) print("max [string]:\t", max(list2)) # Check first alphabet only. # min : This function returns the item from list have smallest value. # min not works for list1 it will throw TypeError. print("min [integer]:\t", min(list3)) print("min [string]:\t", min(list2)) # Check first alphabet only. # append : This function add new item to the list at end. list1.append('Japan') print("append: ", list1) # insert : This function inserts item into list at offset index. list1.insert(3, 'China') print("insert: ", list1) list1.insert(0, 'Pakistan') print("insert: ", list1) # reverse : This function reverses items of list in place. list1.reverse() print("reverse: ", list1) # count : This function returns count of how many times item # exist in the list. print("count: ", list1.count('Pakistan')) print("count: ", list1.count('pakistan')) print("count: ", list1.count(4000)) print("count: ", list1.count('4000')) # index : This function appends one list to another # If item not found in list it will throw ValueError print("index: ", list1.index(4000)) print("index: ", list3.index("Red")) print("index: ", list3.index("Yellow")) # pop : This function removes and returns last item # from list list1.pop() print("pop: ", list1) list1.pop() print("pop: ", list1) # sort : This function Sorts the items in the list so they appear in # ascending order (from the lowest value to the highest value). # if we sort list1 it will show below error # builtins.TypeError: unorderable types: int() < str() list3.sort() print("sort: ", list3) list2.sort() print("sort: ", list2) # extend : This function appends one list to another list2.extend(list3) print("extend: ", list2) # list : This Function converts a tuple into list. tup1 = ('India', 'Germany', 5000, 1000, 'Japan') list4 = list(tup1) print("list: \t", list4) main()
Sample output of above program.
3.5.2 |Anaconda 4.2.0 (32-bit)| (default, Jul 5 2016, 11:45:57) [MSC v.1900 32 bit (Intel)]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate lists.py]
########################################################
len : 6
max [integer]: Yellow
max [string]: 5000
min [integer]: Blue
min [string]: 1000
append: ['India', 'Germany', 251, 4000, 'Australia', 5478, 'Japan']
insert: ['India', 'Germany', 251, 'China', 4000, 'Australia', 5478, 'Japan']
insert: ['Pakistan', 'India', 'Germany', 251, 'China', 4000, 'Australia', 5478, 'Japan']
reverse: ['Japan', 5478, 'Australia', 4000, 'China', 251, 'Germany', 'India', 'Pakistan']
count: 1
count: 0
count: 1
count: 0
extend: [1000, 2000, 3000, 4000, 5000, 'Yellow', 'Blue', 'Green', 'Red']
index: 3
index: 3
index: 0
pop: ['Japan', 5478, 'Australia', 4000, 'China', 251, 'Germany', 'India']
pop: ['Japan', 5478, 'Australia', 4000, 'China', 251, 'Germany']
list: ['India', 'Germany', 5000, 1000, 'Japan']
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate lists.py]
########################################################
len : 6
max [integer]: Yellow
max [string]: 5000
min [integer]: Blue
min [string]: 1000
append: ['India', 'Germany', 251, 4000, 'Australia', 5478, 'Japan']
insert: ['India', 'Germany', 251, 'China', 4000, 'Australia', 5478, 'Japan']
insert: ['Pakistan', 'India', 'Germany', 251, 'China', 4000, 'Australia', 5478, 'Japan']
reverse: ['Japan', 5478, 'Australia', 4000, 'China', 251, 'Germany', 'India', 'Pakistan']
count: 1
count: 0
count: 1
count: 0
extend: [1000, 2000, 3000, 4000, 5000, 'Yellow', 'Blue', 'Green', 'Red']
index: 3
index: 3
index: 0
pop: ['Japan', 5478, 'Australia', 4000, 'China', 251, 'Germany', 'India']
pop: ['Japan', 5478, 'Australia', 4000, 'China', 251, 'Germany']
list: ['India', 'Germany', 5000, 1000, 'Japan']
2017-08-15T13:53:52+05:30
2017-08-15T13:53:52+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution