# List

1. Lists are one of the most powerful tools in Python.
2. They are just like the arrays declared in other languages.
3. But the most powerful thing is that list need not be always homogeneous.
4. A single list can contain strings, integers, as well as objects.
5. Lists can also be used for implementing stacks and queues.
6. Lists are mutable, i.e., they can be altered once declared.

Example - 1:

```python
# Declaring and printing list 
L = [1, "a" , "string" , 1+2] 
print(L)   
```

\[1, 'a', 'string', 3]

Example - 2:

```python
l = [ 'hello','hi','how are you' ] # Homogenous List(contains element of same type)
    #  0       1      2    (indexing)
print(l)
print(l[2]) #print element at index 2
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12] #Homogenous list
print("Homogeneous List :",l)
print(l[5])
l = [ 'hello','hi', 3, 4.5 ] #Non-homogeneous List (contains element of differnt type)
#       -4   -3   -2     -1
print("Non-Homogeneous List : ",l)
print(l[-3])
```

\['hello', 'hi', 'how are you']

how are you

Homogeneous List : \[56, 23, 45, 12, 67, 12, 43, 1, 6, 8, 6, 33, 12]

12

Non-Homogeneous List : \['hello', 'hi', 3, 4.5]

hi

Example - 3:

```python
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]
print(l[2:10])  #SLICING (print element from index 0 to n-1) here it prints element from index 2 to 9
print("Items in original list : ",len(l))
x = None 
y = None
z = -1
print("Reverse of List ",l[x:y:z])  #([starting_point:ending_point:step]) here starting and ending point is none and step is -1
x = -2
y = -9
z = -1
print(l[x:y:z])
```

\[45, 12, 67, 12, 43, 1, 6, 8]

Items in original list : 13

Reverse of List \[12, 33, 6, 8, 6, 1, 43, 12, 67, 12, 45, 23, 56]

\[33, 6, 8, 6, 1, 43, 12]

**Operation on a list to Add items**

1. append
2. insert
3. extend

**To Delete items**

1. pop
2. remove

**To Common Operation**

1. sort
2. copy
3. reverse
4. count
5. index
6. clear

## Build\_in Functions to Add Items <a href="#bulid_in-functions-to-add-items" id="bulid_in-functions-to-add-items"></a>

Example - 5:

```python
#append(item) insert element at the end of the list
l = [ 'hi',3.14,'how']
l.append('item')
print(l)
l.append(122335)
print(l)
k = [ 9,8,7,6,5,4,3,]
l.append(k)
print(l)
```

\['hi', 3.14, 'how', 'item']

\['hi', 3.14, 'how', 'item', 122335]

\['hi', 3.14, 'how', 'item', 122335, \[9, 8, 7, 6, 5, 4, 3]]

Example - 6:

```python
#insert(index,item) insert item at given index 
l = [ 'hello', 3.14,1452,432423,434,234]
l.insert(2,'hi')
print(l)
```

\['hello', 3.14, 'hi', 1452, 432423, 434, 234]

Example - 7:

```python
#extend(sequence) 
l = []
l.extend('python')
print(l)
```

\['p', 'y', 't', 'h', 'o', 'n']

Example - 8:

```python
l = ['ghc']
k = [ 'java','c','c++','python'] 
l.extend(k)
print(l)
```

\['java', 'c', 'c++', 'python']

## **B**uild\_in Functions to Remove Items <a href="#bulid_in-functions-to-remove-items" id="bulid_in-functions-to-remove-items"></a>

Example - 9:

```python
#delete item from list
l = [ 5,6,67,5324,324]
print(l)
x = l.pop() #just opposite of append i.e delete the last element in list
print("Item ",x,"is deleted from list")
print(l)
```

\[5, 6, 67, 5324, 324]

Item 324 is deleted from list

\[5, 6, 67, 5324]

Example - 10:

```python
#pop(index)
l = [10,9,9,34,2,43213,21]
print(l)
k = l.pop(5)
print("item at index 5 is {} is deleted".format(k))
print(l)
```

\[10, 9, 9, 34, 2, 43213, 21]

item at index 5 is 43213 is deleted

\[10, 9, 9, 34, 2, 21]

Example - 11:

```python
#remove(item) remove particular item from list
l = [ 'hi','hello','how are you','hi','hi']
print(l)
l.remove('hi')
print(l)
```

\['hi', 'hello', 'how are you', 'hi', 'hi']

\['hello', 'how are you', 'hi', 'hi']

## Practice Problems: <a href="#common-functions" id="common-functions"></a>

Example - 12:

```python
#Common Function on Python List
#sort()-> it will sort list in accending ( list should be homogenous )
#sort(reverse=True)->it will sort in decending order
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]
k = l #k uses the same address of l
print(k)
k.sort() #sort the list k
print("Sorted k : ",k)
print(l)
l.sort(reverse=True)
print("Sorted l : ",l)
```

\[56, 23, 45, 12, 67, 12, 43, 1, 6, 8, 6, 33, 12]

Sorted k : \[1, 6, 6, 8, 12, 12, 12, 23, 33, 43, 45, 56, 67]

\[1, 6, 6, 8, 12, 12, 12, 23, 33, 43, 45, 56, 67]

Sorted l : \[67, 56, 45, 43, 33, 23, 12, 12, 12, 8, 6, 6, 1]

Example - 13:

```python
l = [ 'zebra','apple','something','eye','flag','computer','python']
k = [ 'zebra','apple','something','eye','flag','computer','python']
print(*l)  #prints only elements in list l 
l.sort()
print(*l)  #print after sorting alphabetically
print(*k)
k.sort(reverse=True)
print(*k)
```

zebra apple something eye flag computer python

apple computer eye flag python something zebra

zebra apple something eye flag computer python

zebra something python flag eye computer apple

Example - 14:

```python
mylist = [ 
    ['hi',10,32],
    ['abc',100,2,3,],
    ['something',5,6,7,8],
    ['popcorn',9,10,11,12]
]
print(mylist)
mylist.sort() #sort according to the element at index 0 of every sublist
print(mylist)
```

\[\['hi', 10, 32], \['abc', 100, 2, 3], \['something', 5, 6, 7, 8], \['popcorn', 9, 10, 11, 12]]

\[\['abc', 100, 2, 3], \['hi', 10, 32], \['popcorn', 9, 10, 11, 12], \['something', 5, 6, 7, 8]]

Example - 15:

```python
#reverse -> Reverse the order of items in the list
l = [ 1,2,3,4,5,6,7]
x = l[::-1] #reverse the list through slicing
print(x)
print(l)
l.reverse()
print(l)
```

\[7, 6, 5, 4, 3, 2, 1]

\[1, 2, 3, 4, 5, 6, 7]

\[7, 6, 5, 4, 3, 2, 1]

Example - 16:

```python
#copy -> Returns a shallow copy of the list
l = [ 1,2,3,4,5]
k = l.copy()
z = l
print(l)
l[2]= 5 #will change only in list l not in their copies
print(k)
print(z)
print(l)
```

\[1, 2, 3, 4, 5]

\[1, 2, 3, 4, 5]

\[1, 2, 5, 4, 5]

\[1, 2, 5, 4, 5]

Example - 17:

```python
#index -> Returns the index of the first matched item
l = [ 12,3,4532,3213,45,423,453241,53,42,4542,4,63243,4542,5,34,3445,3,34,34,234,324]
k = l.index(4542)
print(k)
print(l[k+2])
l = [ 'hello','hi',12,23,23,[1,23],'hi']
k = l.index('hi')
j = l.index([1,23])
print(k)
print(j)

```

9

63243

1

5

Example - 18:

```python
#clear -> Removes all items from the list
mylist = [ 'hello','hi','bye',1,2,3]
print(mylist)
mylist.clear()
print(mylist)
```

\['hello', 'hi', 'bye', 1, 2, 3]

\[]

Example - 19:

```python
#count -> Returns the count of number of items passed as an argument
l = [ 1,6,3,2,1,34,64,1,3,5654,1,344,62,1,346,3,1,35464,1,35235,1,4352,3]
print(l)
x = l.count(3)
y = l.count(1)
print(x)
print(y)
```

\[1, 6, 3, 2, 1, 34, 64, 1, 3, 5654, 1, 344, 62, 1, 346, 3, 1, 35464, 1, 35235, 1, 4352, 3]

4

8


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-python.gitbook.io/python-programming/python-data-types/list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
