Python List

Estimated reading: 4 minutes 1061 views

List Overview

Lists are used to store multiple items in a single variable.

List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has index [1] etc.

When we say that lists are ordered, it means that the items have a defined order, and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.

Since lists are indexed, lists can have items with the same value means duplicates are allowed


					list1 = [1,2,3,4,3,1]


Python List looping indexing Static way

In this Example we will pass the hard coded the starting and ending limit , which is treated as Static Looping , which is not advisable.

					list1 = [1,2,3,4,5]
for i in range(0,5):

Python List looping indexing Dynamic way

here using The len() function of list we can easily loop dynamic wise.

					list1 = [10,20,33,44,55]
for i in range(0,len(list1)):

Looping the list without len

The below code shows direct looping.

					list1 = [10,22,55,66]
for i in list1:

Accessing the items in list

The below code shows the indexing of list

List len() function

					list1 = [10,20,30,40]

len function is used to get the lengeth of the given list

					list1 = [10,20,30,40]

list count()

The count() is used to count the number of occurence of given value in list.

					list1 = [10,20,30,40,10]
num = 10
res1 = list1.count(num)


Change Item Value

To change the value of a specific item, refer to the index number:
					thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"

Change a Range of Item Values

To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values:
					thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]

List Append()

the append() is used to add the items last in the list
					list1 = [10,20,30]

List insert()

To insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index:
					thislist = [1,2,3]
thislist.insert(1, 7)

List remove()

The remove() method removes the specified item.

if duplicate items is there it will only delete the first occurence.

					thislist = [7,9,10]

List del()

del() with take the parameter as index and delete the value from list for specified position.

					list1 = [10,20,30,50]
del list1[0]

List Without Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.


Based on a list of Game, you want a new list, containing only the Game with the character “c” in the Game list.

Without list comprehension you will have to write a for statement with a conditional test inside:

					Game_list = ["cricket","football","carme","Ludo"]
newlist = []

for x in Game_list:
  if "c" in x:


List sort()

The sort() is used to sort the list in descending order
					list1 = [1,5,4,7,5,9]

List copy()

You cannot copy a list simply by typing l2 = l1, because: l2 here only be a reference to l1, and changes done in l1 will automatically also be made in l2. There are ways to make a copy, one way is to use the built-in List method copy().
					l1 = [10,20,30,40]
res = l1.copy()

Join Two Lists using '+' operators

Using ‘+’ operator we can concate two lists.

					list1 = [10,20,30]
list2 = [55,44,66]
res = list1 + list2

Join Two Lists using extends Function

Use the extend() method to add list2 at the end of list1:

					list1 = [7,8,9]
list2 = [1, 2, 3]


List reverse() Method

reverse() , is used to reverse the list

					list1 = [1,2,3,4,3]

Leave a Comment