DEV Community

Arokya Naresh
Arokya Naresh

Posted on

methods in python 31.07.2024

Methods in LIST

1.Append method

this append method will add items/info at the end of the list

Eg:
package=['Apple','Mango','Banana']
print(package)

package.append('Kiwi')
print(package)

o/p
['Apple', 'Mango', 'Banana']
['Apple', 'Mango', 'Banana', 'Kiwi']

2.insert method

If incase you need to add the items/info in the middle so we can use insert() method

package=['Apple','Mango','Banana']
print(package)

package.insert(1,'Guava') #1 is the index position where we can add items/info
print(package)

o/p
['Apple', 'Mango', 'Banana']
['Apple', 'Guava', 'Mango', 'Banana']

3.Remove method

package=['Apple','Mango','Banana']
print(package)

package.remove('Mango')
print(package)

o/p:
['Apple', 'Mango', 'Banana']
['Apple', 'Banana'

4.Pop method

This POP method will removes last items/info in the list
Also deleted items/info will be returned or assigned to a variiable

Eg:
package=['Apple','Mango','Banana']
print(package)

package.pop()
print(package)

O/P
['Apple', 'Mango', 'Banana']
['Apple', 'Mango']

5.Index method()
To find the Index position we use index() method
Eg:
package=['Apple','Mango','Banana']
print(package)

x=package.index('Mango')
print(x)
o/p
['Apple', 'Mango', 'Banana']
1

6.sort() method

It is used to sort the items/values in the list in Alphabetic order

Eg:
package=['Apple','Mango','Banana']
print(package)

package.sort()
print(package)

O/P
['Apple', 'Mango', 'Banana']
['Apple', 'Banana', 'Mango']

7.reverse() method

It is used to reverse the items/values in the list

Eg
package=['Apple','Mango','Banana']
print(package)

package.reverse()
print(package)

O/P
['Apple', 'Mango', 'Banana']
['Banana', 'Mango', 'Apple']

Top comments (1)

Collapse
 
nlxdodge profile image
NLxDoDge

Hi welcome to DEV.to.

Here are some tips to make your post even better 😉

You can follow the standard markdown options to make code blocks like this:

package=['Apple','Mango','Banana']
print(package)

package.sort()
print(package)
Enter fullscreen mode Exit fullscreen mode

With three backticks (and the language for nice colors like this:

Image description