Python List Organization
li = [ 'Mars', 'Mercury', 'Venus', 'Earth' ]
# list unorder of distance from the Sun
indx = li.index('Mars') # This line finds the index of the element 'Mars' in the list li
item = li.pop(indx) #This line removes the element at the index indx from the list li
li.append(item)
print(li)
#Output : ['Mercury', 'Venus', 'Earth', 'Mars']
As you can see from the above, We employed the following advice to accomplish the List Organization:
- index() method : The index() method in Python lists is used to find the index of the first occurrence of a specified value in the list. If the value is not found, it raises a ValueError
- pop() method : The pop() method in Python lists is used to remove and return the element at a specified index. If no index is specified, it removes and returns the last element of the list
Comments
Post a Comment