# Python List

Estimated reading: 4 minutes 1184 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]
print(list1

```
```

## 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):
print(list1[i])
```
```

## 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)):
print(list[i])
```
```

## Looping the list without len

The below code shows direct looping.

```				```
list1 = [10,22,55,66]
for i in list1:
print(i)
```
```

## Accessing the items in list

The below code shows the indexing of list

## List len() function

```				```
list1 = [10,20,30,40]
print(list1[0])
print(list1[1])
print(list1[2])
print(list1[3])
```
```

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

```				```
list1 = [10,20,30,40]
print(len(list1))
```
```

## 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)
print(res1)

```
```

## Change Item Value

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

## 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"]
print(thislist)
```
```

## List Append()

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

## 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)
print(thislist)
```
```

## 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]
thislist.remove(10)
print(thislist)
```
```

## 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]
print(list1)
```
```

## 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.

Example:

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:
newlist.append(x)

print(newlist)
```
```

## List sort()

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

## 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()
print(res)
```
```

## Join Two Lists using '+' operators

Using ‘+’ operator we can concate two lists.

```				```
list1 = [10,20,30]
list2 = [55,44,66]
res = list1 + list2
print(res)
```
```

## 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]

list1.extend(list2)
print(list1)
```
```

## List reverse() Method

reverse() , is used to reverse the list

```				```
list1 = [1,2,3,4,3]
list1.reverse()
print(list1)
```
```