Loading, please wait...

Python – Lists

A list is the collection which is ordered and changeable. In Python, lists are written with square brackets.

Example

Create a List:

thislist = ["apple", "banana", "cherry"]
print(thislist)

 Output:

C:\Users\My Name>python demo_list.py
['apple', 'banana', 'cherry']

 

Example

Append the List:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

 

List Methods

There are the set of built-in methods that you can use on lists.

Method

Description

append()

It Add an element at the end of the list

clear()

It Removes all the elements from the list

copy()

It Returns a copy of the list

count()

It Returns the number of elements with the specified value

extend()

It Add the elements of a list (or any iterable), to the end of the current list

index()

It Returns the index of the first element with the specified value

insert()

It Adds an element at the specified position

pop()

It Removes the element at the specified position

remove()

It Removes the item with the specified value

reverse()

It Reverses the order of the list

sort()

It Sorts the list