Python Tutorial | Python List

How to change or add elements to a list?

List are mutable, meaning, their elements can be changed unlike string or tuple.
We can use assignment operator (=) to change an item or a range of items.
Positive index from left to right (starts from 0)

Example:1

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

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

output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example:2

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[2]=300
n[6]=200
n[8]=80
print(n)

Group value changes

Example:3

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[1:4]=[10,20,30]
#position changed will be 1,2,3 (start to pos-1)
print(n)

Example:4

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:8]=[100,200,300]
print(n)

Example:5

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:8]=[100,200,300]
print(n)

Example:6

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:9]=[100,200,300]
print(n)
Note: 4 locations are replaced by 3 values

Example:7

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:9]=[100,200]
print(n)
Note: 4 locations are replaced by 2 values

Example:8

0

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

10

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:9]=[100]
print(n)
Note: 4 locations are replaced by 1 value

Example:9

n = [1,2,3,4,5,6,7,8,9,10]
print(n)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# change 2nd to 4th items
n[1:4] = [20,30,40,50]
Note: 3 locations are replaced by 4 values
print(n)

Example:10

n = [1,2,3,4,5,6,7,8,9,10]
n[5:8]=[50,60,70,80]
print(n)
Note: 3 locations are replaced by 4 values

Example:11

n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:7]=[100,200,300,400,500]
print(n)
Note: 2 locations are replaced by 5 values

Example:12 (Solve)

n=[1,2,3,4,5,6,7,8,9,10]
print(n) #1
n[1:3]=[100]
print(n) #2
n[2:5]=[100,200]
print(n) #3
n[6:9]=[500,700,900]
print(n) #4
n[2:6]=[170,340]
print(n) #5

List Concatenation Operator (+)

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

Example:13

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

Output:

[1, 3, 5, 9, 7, 5]

Example:14

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)
print(n+[10,20,30])

Output:

list n
[1, 2, 3, 4, 5]
list n1
[6, 7, 8, 9, 10]
n+n1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 10, 20, 30]

For Practice

Question :

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)

output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]

Question:

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)

Output:

[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 100, 200, 300]
[1, 2, 3, 4, 5, 100, 200, 300, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 100, 200, 300, 6, 7, 8, 9, 10, 23, 45, 56, 1, 2, 3, 4, 5, 100, 200, 300]

Visitor Counter

0056706