Python List activities example: Here, we will find out about the different list tasks like getting to components, printing components, switching components, embeddings, and so forth in Python.
Here, we are executing a python program for different list activities, the following tasks are being acted in the list,
- Pronouncing an integer list
- Printing the total list
- Printing the principal component of the list
- Printing ith component of the list
- Printing components inside the given files
- Printing a particular component utilizing the negative ordering
- Attaching a component to the list
- Finding the file of a particular component in the list
- Arranging the list components
- Popping a component from the list
- Expelling indicated component from the list
- Embeddings a component at indicated file
- Broadening the list, for example, embed set of component (list) in the list
- Turning around list components
Python code for different list activity:
# Python code for various list operation
# declaring a list of integers
iList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# List slicing operations
# printing the complete list
print('iList: ',iList)
# printing first element
print('first element: ',iList[0])
# printing fourth element
print('fourth element: ',iList[3])
# printing list elements from 0th index to 4th index
print('iList elements from 0 to 4 index:',iList[0: 5])
# printing list -7th or 3rd element from the list
print('3rd or -7th element:',iList[-7])
# appending an element to the list
iList.append(111)
print('iList after append():',iList)
# finding index of a specified element
print('index of \'80\': ',iList.index(80))
# sorting the elements of iLIst
iList.sort()
print('after sorting: ', iList);
# popping an element
print('Popped elements is: ',iList.pop())
print('after pop(): ', iList);
# removing specified element
iList.remove(80)
print('after removing \'80\': ',iList)
# inserting an element at specified index
# inserting 100 at 2nd index
iList.insert(2, 100)
print('after insert: ', iList)
# counting occurances of a specified element
print('number of occurences of \'100\': ', iList.count(100))
# extending elements i.e. inserting a list to the list
iList.extend([11, 22, 33])
print('after extending:', iList)
#reversing the list
iList.reverse()
print('after reversing:', iList)
Output:
iList: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
first element: 10
fourth element: 40
iList elements from 0 to 4 index: [10, 20, 30, 40, 50]
3rd or -7th element: 40
iList after append(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111]
index of '80': 7
after sorting: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111]
Popped elements is: 111
after pop(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
after removing '80': [10, 20, 30, 40, 50, 60, 70, 90, 100]
after insert: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100]
number of occurences of '100': 2
after extending: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100, 11, 22, 33]
after reversing: [33, 22, 11, 100, 90, 70, 60, 50, 40, 30, 100, 20, 10]