Lists In Python
Lists
The List is one of the most widely utilized collection in Python programs. The list is more like an array. The difference between an array and a list is that a list in Python may have different types of elements but an array has all the elements of the same type. List are both mutable and a sequence data type that permits them to be indexed. The list can have different types of objects, including other list-objects.
l=[12,5,21,3.1,'python','c',0.399, 300]
The members of the list are contained within [] brackets(square).
A list can be accessed by indices.
Basic operations on a list
The general operation those can be performed on lists are print, iterate, append, pop, remove, reverse, count, sort, etc)
Iterating list
We can iterate them with for loop,
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] for x in list: print(x) Output: 23 Python 4.78 programming a b 456 True 11 -45
Accessing list with index position
We can randomly access a list's elements by using its indices like
We can use negative index also like(-1 is last index,-2 is second last and so-on)
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] print('Element at index 3 is ',list[3]) print('') print('Elements in range of index 2 to 7 are ',list[2:7]) Output: Element at index 3 is programming Elements in range of index 2 to 7 are [4.78, 'programming', 'a', 'b', 456]
We can use negative index also like(-1 is last index,-2 is second last and so-on)
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] print('|Elements [-6:-2]|',list[-6:-2]) Output: |Elements [-6:-2]| ['a', 'b', 456, True]
append() method
We can append a list as lists are dynamic data structures
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] list.append('dyanmic') print(list) Output: [23, 'Python', 4.78, 'programming', 'a', 'b', 456, True, 11, -45, 'dyanmic']
pop() method
We can remove the last element from list using the pop() function
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] list.pop() print(list) Output: [23, 'Python', 4.78, 'programming', 'a', 'b', 456, True, 11]
remove() method
We can remove any element by its value from the list
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] list.remove('b') list.remove(4.78) print(list) Output: [23, 'Python', 'programming', 'a', 456, True, 11, -45]
reverse() method
We can also reverse a list by the reverse() method.
list=[23,'Python',4.78,'programming','a','b',456,True,11,-45] list.reverse() print('Reversed list is ',list) Output: Reversed list is [-45, 11, True, 456, 'b', 'a', 'programming', 4.78, 'Python', 23]
count() method
This method can be used to count the number occurrence of an Element in a list.
list=[23,'Python',4.78,'programming','a','a',456,True,11,-45] count=list.count('a') print('Total elements are ',count) Output: Total elements are 2
sort() method
list=[23,25,12,19,110,11,-45] list.sort() print('sorted list',list) Output: sorted list [-45, 11, 12, 19, 23, 25, 110]