# List

list data type:
If we want to represent a group of values as a single entity where insertion order required to preserve and duplicates are allowed then we should go for list data type.

1. insertion order is preserved
2. heterogeneous objects are allowed
3. duplicates are allowed
4. Growable in nature
5. values should be enclosed within square brackets.

Why List is dynamic in nature?

List is dynamic because based on our requirement we can increase the size and decrease the size.
In List the elements will be placed within square brackets and with comma seperator.

List vs mutability:
List objects are mutable.
Eg:
1) n=[10,20]
2) print(n)
3) n=77
4) print(n)
5)
6) D:\Python_classes>py test.py
7) [10, 20]
8) [10, 777]

To display only even numbers:
1) n=[0,1,2,3,4,5,6,7,8]
2) for n1 in n:
3) if n1%2==0:
4) print(n1)
5)
6) D:\Python_classes>py test.py
7) 0
8) 2
9) 4
10) 6
11) 8

To display elements by index wise:
1) l=[“d”,”e”]
2) x=len(l)
3) for i in range(x):
4) print(l[i],”is available at positive index: “,i,”and at negative index: “,i-x)
5)
6) Output
7) D:\Python_classes>py test.py
8) d is available at positive index: 0 and at negative index: -2
9) e is available at positive index: 1 and at negative index: -1

###### Explain Python’s characteristics:
• it is an ordered and mutable type.
• its element can be accessed by index.
• it can store a number of various elements.
• its insertion order preserved.
• In list, duplicate objects are allowed
• In List, heterogeneous objects are allowed.
###### What is the slice operator [] in python?

The elements of the list can be accessed by the slice operator [].

``````list_var(start:stop:step)
print(list[2:10:2])  --4,6,8,10

list=[1,2,3,4]
print(list[:]) =1,2,3,4

list = [3,4,5]
print(list[-1])  ---5
print(list[-2:])  --4,5
print(list[:-1])  --3,4
print(list[-2:-1]) --5

list = [1, 2, 3, 4]
print(list)  ---   1,2,3,4
list = 12
print(list)  ---  1,2,12,4
list[1:3] = [18, 17]
print(list)  --- 1,18,12,17
list[-1] = 15
print(list)  ---1,18,12,15

``````
##### What is frozenset Data Type?

It is exactly same as set except that it is immutable.
Hence we cannot use add or remove functions.
1) >>> s={10,20,30,40}
2) >>> fs=frozenset(s)
3) >>> type(fs)
4)
5) >>> fs
6) frozenset({40, 10, 20, 30})
7) >>> for i in fs:print(i)
8) …
9) 40
10) 10
11) 20
12) 30

###### How will you remove the duplicate element of the list?
``````list12 = [10,20,20,3,55]
list21 = []
for i in list12:
if i not in list21:
list21.append(i)
print(list21) ``````
###### Write the program to find the lists consist of at least one common element.
``````list1 = [1,2,3]
list2 = [7,8,9]
for x in list1:
for y in list2:
if x == y:
print("The common is:",x) ``````
###### Write a program to create a list of 6 odd integers.Replace the fourth element with a list of 3 even integers.After that perform Flatten, sort and print the list.
``````
x = [1, 3, 5, 7, 9,11]
y = [2, 4, 6]
x = y
print(x)
x = x[:3] + [*y] + x[4:]
print(x)
x.sort()
print(x)

Output

[1, 3, 5,[2, 4, 6], 9,11]

[1, 3, 5,2, 4, 6, 9,11]

[1, 2, 3, 4,5, 6,  9,11]
``````
###### A list contains 10 integers generated randomly. Give any input and show its position in the list.
``````import random
list1 = []
for k in range(10):
n = random.randint(0, 40)
list1.append(n)
print(list1)

num1 = int(input('Enter number:'))
count = 0
for i in range(len(list1)):
if list1[i] == num1:
print('Number found at position:', i)``````
###### Suppose a list has 10 numbers. Write a program to removes duplicates.
``````list1 = [1, 2, 3, 4, 3, 4, 5, 10, 20, 68]
print('list: ', list1)
final_list1=[]
for num in list1:
if num not in final_list1:
final_list1.append(num)

list1 = final_list1
print('List after removing duplicates:', list1)``````
###### In Class 6, some of the students got negative marks and some students got positive marks. How will you create lists of positive and negative Numbers separately?
``````
list11 = [10, -19, -26, -40, -70,-10, 20, 30, 14, 15]
list12 = []
list13 = []
count, ncount = 0, 0
for x in list11:
if x >= 0:
list12.append(x)
else:
list13.append(x)

print(' list:', list11)
print('Positive Numbers list:', list12)
print('Negative Numbers list:', list13)``````
###### How will you convert list into uppercase?
``````list1 = ['abc', 'def']
for i, item in exerate(list1):
list1[i] = item.upper()
print(list1)
Output
[‘ABC’, ‘DEF’]``````
###### Can we create empty list object?
``````list=[]
print(list)``````
###### Explain split() function.
``````x="Learning java"
l=x.split()
print(l)

['Learning', 'java',]``````
###### How List is mutable?Explain any example.
``````1) x=[10,20]
2) print(n)
3) n=777
4) print(n)
--[10, 777]``````

Important functions of List:
len():returns the number of elements available in the list

count():
returns the number of occurrences of specified item in the list

index() function:
returns the index of first occurrence of the specified item.

If the specified index is greater than max index then element will be inserted at last position.

If the specified index is smaller than min index then element will be inserted at
first position.

append() function:
can use append() function to add item at the end of the list.

1) list=[]
2) list.append(“D”)
3) list.append(“E”)
4) list.append(“F”)
5) print(list)
6)
7) D:\Python_cloud>py test.py
8) [‘D’, ‘E’, ‘F’]

insert() function:
To insert item at specified index position

1) n=[1,2,3,4,50]
2) n.insert(1,88)

Differences between append() and insert()

APPEND-In List when we add any element it will come in last i.e. it will be last element.
INSERT-In List we can insert any element in particular index number

extend() function:
To add all items of one list to another list Then we can use extend function.

l1.extend(l2)
all items present in l2 will be added to l1
Eg:
1) list1=[“test1″,”test2″,”test3”]
2) order2=[“test4″,”test5″,”test6”]
3) list1.extend(order2)
4) print(list1)
5)
6) D:\Python_classes>py test.py
7) [‘test1’, ‘test2’, ‘test3’, ‘test4’, ‘test5’, ‘test6’]

remove() function:
can use this function to remove specified item from the list.

If the item present
multiple times then only first occurrence will be removed.

pop() function:
It removes and returns the last element of the list.
If the list is empty then pop() function raises IndexError

1) n=[10,20,30,40]
2) print(n.pop())
3) print(n.pop())
4) print(n)
5)
6) D:\Python_classes>py test.py
7) 40
8) 30
9) [10, 20]

If the list is empty then pop() function raises IndexError
Eg:
1) n=[]
2) print(n.pop()) ==> IndexError: pop from empty list

we can use pop() function to remove last element of the list. But we can use to
remove elements based on index. also
n.pop(index)===>To remove and return element present at specified index.
n.pop()==>To remove and return last element of the list
1) n=[10,30,50,60]
2) print(n.pop()) #60
3) print(n.pop(1)) #20
4) print(n.pop(20)) ==>IndexError: pop index out of range

Differences between remove() and pop()
remove() pop()
1) We can use remove to remove special element from the List.
1) We can use pop to remove last element from the List.
2) remove can’t return any value.
2) pop returned removed element.
3) In Remove,If special element not available then we get VALUE ERROR.
3) In Pop,If List is empty then we get Error.

We came to know that List objects are dynamic. i.e based on our requirement we can increase and decrease the
size.
append(),insert() ,extend() ===>for increasing the size/growable nature
remove(), pop() ======>for decreasing the size /shrinking nature

reverse():
We can use to reverse() order of elements of list.

1) n=[10,20,50,40]
2) n.reverse()
3) print(n)
4)
5) D:\Python_classes>py test.py
6) [40, 50, 20, 10]

sort() function:
In list by default insertion order is preserved. If want to sort the elements of list according
to default natural sorting order then we should go for sort() method.

Aliasing and Cloning of List objects in python:
As we know rhat the process of giving another reference variable to the existing list is called aliasing.
Eg:
1) x=[10,20,30]
2) y=x
3) print(id(x))
4) print(id(y))
by using one reference variable if we are changing content,then those changes will be reflected to the other reference variable.
1) x=[10,20,30]
2) y=x
3) y=111
4) print(x) ==>[10,111,30]

For that we can go for cloning.

We can implement cloning by using slice operator or by using copy() function

By using slice operator:
1) x=[10,20,30,40]
2) y=x[:]
3) y=11
4) print(x) ==>[10,20,30,40]
5) print(y) ==>[10,11,30,40]

By using copy() function:
1) x=[10,20,30,40]
2) y=x.copy()
3) y=11
4) print(x) ==>[10,20,30,40]
5) print(y) ==>[10,11,30,40]