# Python Tutorial | Python List

Python List Methods

Methods that are available with list object in Python programming are given below.
They are accessed as list.method().

append() – Add an element to the end of the list

#append method
n=[1,2,3,4,5]
print(n)
#[1, 2, 3, 4, 5]
n.append(6)
print(n)
#[1, 2, 3, 4, 5, 6]

extend() – Add all elements of a list to the another list

#extend() method
n=[1,2,3,4,5]
print(n)
#[1, 2, 3, 4, 5]
n.extend([6,7,8])
print(n)
#[1, 2, 3, 4, 5, 6, 7, 8]

insert() – Insert an item at the defined index

#insert method
Example:
n=[1,2,3,4,5]
print(n)
#[1, 2, 3, 4, 5]

n.insert(1,10)
print(n)
#[1, 10, 2, 3, 4, 5]

n.insert(4,40)
print(n)
#[1, 10, 2, 3, 40, 4, 5]

Furthermore, we can insert one item at a desired location by using the method insert() or insert multiple items by squeezing it into an empty slice of a list.

n = [1, 9]
n.insert(1,3)
print(n)

#Output:
[1, 3, 9]

n[2:2]=[5, 7]
print(n)

# Output:
[1, 3, 5, 7, 9]

We can also use + operator to combine two lists. This is also called concatenation.

n = [1, 3, 5]
print(n + [9, 7, 5])

#Output:
[1, 3, 5, 9, 7, 5]

Example for pratice:

n=[1,2,3,4,5]
n1=[6,7,8,9,10]
print(“list n”)
print(n)
print(“list n1”)
print(n1)
print(“n+n1”)
print(n+n1)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(n+[10,20,30])

Example for pratice:

n1=[1,2,3,4,5]
n2=[6,7,8,9,10]
print(n1)
print(n2)
print(n1+n2)
n=n1+n2
print(n1)
print(n2)
print(n)
n=n2+n1
print(n)

Example for pratice:

n1=[1,2,3,4,5]
n2=[6,7,8,9,10]
print(n1)
print(n2)
n1=n1+[100,200,300]
print(n1)
n2=n1+n2
print(n2)
n2=n2+[23,45,56]+n1
print(n2)

The * operator repeats a list for the given number of times.

print([“hello”]*3)
#Output:
[‘hello’, ‘hello’, ‘hello’]

Without list

print(“hello”*3)
#Output:
hellohellohello

index() – Returns the index of the first matched item

n = [3, 8, 1, 6, 0, 8, 1,20,1]
print(n.index(8))
#Output:
1

print(n.index(6))
#output:
3

print(n.index(1))
#Output:
2

count() – Returns the count of number of items passed as an argument

n = [3, 8, 1, 6, 0, 8, 1,20,1]

print(n.count(1))
#Output:
3

print(n.count(8))
#Output:
2

print(n.count(20))
#Output:
1

print(n.count(67))
#Output:
0

sort() – Sort items in a list in ascending order

n = [3, 8, 1, 6, 0, 8, 1,20,1]
print(n)
n.sort()
print(n)
#output:
[3, 8, 1, 6, 0, 8, 1, 20, 1]
[0, 1, 1, 1, 3, 6, 8, 8, 20]
>>>

n=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n)
n.sort()
print(n)
#Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘e’, ‘m’, ‘o’, ‘p’, ‘r’, ‘t’, ‘u’]
>>>

reverse() – Reverse the order of items in the list

n = [3, 8, 1, 6, 0, 8, 1,20,1]
print(n)
n.reverse()
print(n)

#Output:
[3, 8, 1, 6, 0, 8, 1, 20, 1]
[1, 20, 1, 8, 0, 6, 1, 8, 3]
>>>

n=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n)
n.reverse()
print(n)
#Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘r’, ‘e’, ‘t’, ‘u’, ‘p’, ‘m’, ‘o’, ‘c’]
>>>

len() : Return the length (the number of items) in the list.

n = [3, 8, 1, 6, 0, 8, 1,20,1]
print(n)
print(len(n))
n1=len(n)
print(“length = “,n1)
#Output:
[3, 8, 1, 6, 0, 8, 1, 20, 1]
9
length = 9
>>>

copy() – Returns a shallow copy of the list

n = [3, 8, 1, 6, 0, 8, 1,20,1]
print(n)
n1=n.copy()
print(n1)
#output:
[3, 8, 1, 6, 0, 8, 1, 20, 1]
[3, 8, 1, 6, 0, 8, 1, 20, 1]

p=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(p)
p1=p.copy()
print(p1)
#output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]

max() : largest valued element
min() : lowest valued element

n=[23,45,65,-78,34,65,8]
print(max(n))
print(min(n))
#Output:
65
-78

print(max(23,45,765,6))
print(min(23,45,65,56,7,78,-7))
#Output:
765
-7

n=[‘a’,’b’,’c’,’d’,’e’]
print(max(n))
print(min(n))
#Output:
e
a
>>>

sum() : calculates sum of all the elements

n = [1, 2, 3, 4, 5]
print(n)
print(sum(n))
s=sum(n)
print(“Sum = “,s)
#Output:
[1, 2, 3, 4, 5]
15
Sum = 15
>>>

Python List Membership Test Using “in” and “not in”

We can test if an item exists in a list or not, using the keyword “in” or “not in”.

n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
print(‘c’ in n1)
print(‘o’ in n1)
print(‘x’ not in n1)
print(‘p’ not in n1)

#Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
True
True
True
False

n=”computer”
print(n)
print(‘c’ in n)
print(‘o’ in n)
print(‘x’ not in n)
print(‘p’ not in n)

#Output:
computer
True
True
True
False

Iterating Through a List

Using a for loop we can iterate through each item in a list.

Example:

```n=[1,2,3,4,5]
for i in n:
print(i)```

Output:

1
2
3
4
5

Example:

```n=["amit","sumit","kapil","mukesh"]
for i in n:
print(i)```

Output:

amit
sumit
kapil
mukesh

Return value from list()

The list() constructor returns a mutable sequence list of elements.
• If no parameters are passed, it creates an empty list
• If text is passed as parameter, it creates a list of elements in the text

print(list())
#creates an empty list

vowels1 = ‘aeiou’
print(vowels1)
#displays as text

vowels2 = ‘aeiou’
print(list(vowels2))
#displays as list

vowels3 = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
print(vowels3)
#displays as list

#Output:

[]
aeiou
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]

List Comprehension: Elegant way to create new List

List comprehension is an elegant and concise way to create new list from an existing list in Python.
List comprehension consists of an expression followed by for statement inside square brackets.

Example:1
To make a list of all even number upto 20

```n=[x for x in range(1,21) if(x%2==0)]
print(n)```

Output:

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Example:2
To make a list of all odd number upto 20

```n=[x for x in range(1,21) if(x%2==1)]
print(n)```

Output:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Example:3
To make a list of all number multiple of 5 upto 100

```n=[x for x in range(1,101) if(x%5==0)]
print(n)```

Output:

[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

Example:4
To make a list with each item being increasing power of 2.

```n = [2 ** x for x in range(10)]
print(n)```

Output:

[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

Important Pages Visitor Counter       