List example:
In [2]: L1=[]
L2=[123,"python",3.7]
L3=[1,2,3]
L4=['A','B', 'C']
print(L1,L2,L3,L4)
[] [123, 'python', 3.7] [1, 2, 3] ['A', 'B', 'C']
In [7]: L=[1,2,3,4,5,6,7,8,9]
L[1:5]
Out[7]: [2, 3, 4, 5]
In [8]: L[-1]
Out[8]: 9
In [17]: print(L3+L4, "&",L4*3)
[1, 2, 3, 'A', 'B', 'C'] & ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']
In [14]: L[-7:-1]
Out[14]: [3, 4, 5, 6, 7, 8]
In [19]: #reverse of list
L[::-1]
Out[19]: [9, 8, 7, 6, 5, 4, 3, 2, 1]
In [31]: #iterating a list
for i in L:
print(i,end=' ')
1 2 3 1 3 4 2 2 1 4 5
In [28]: len(L)
Out[28]: 9
In [29]: print(max(L),min(L),sum(L))
9 1 45
In [30]: str="computer programming"
print(list(str))
['c', 'o', 'm', 'p', 'u', 't', 'e', 'r', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n',
'g']
In [38]: lst=[5,4,3,1]
lst1=["b","c","a"]
print(sorted(lst),sorted(lst1,reverse=-1))
[Link](reverse=True)
print(lst)
[1, 3, 4, 5] ['c', 'b', 'a']
[5, 4, 3, 1]
In [40]: L=[1,2,3,1,3,4,2,2,1,4,5]
print([Link](4),[Link](2))
2 3
In [43]: print([Link]('c'))
In [46]: [Link](3,'d')
print(lst1)
['b', 'c', 'a', 'd', 'd']
In [49]: [Link](3)
print(lst1)
['b', 'c', 'a', 'd']
In [30]: l1=[1,2,3]
l2=[x*x for x in l1]
print(l2)
[1, 4, 9]
Assignment-4
Q1: Write a Python program to create a list of 5 fruits. Print the first fruit, the last fruit, and the
fruit at index 2.
In [55]: fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print("First fruit:", fruits[0])
print("Last fruit:", fruits[-1])
print("Fruit at index 2:", fruits[2])
First fruit: Apple
Last fruit: Grapes
Fruit at index 2: Mango
Q2: Write a Python program that stores numbers 1 to 10 in a list. Print the first 3 numbers, the
last 3 numbers, and every alternate number.
In [54]: num= [1,2,3,4,5,6,7,8,9,10]
print("First 3 numbers:", num[:3])
print("Last 3 numbers:", num[-3:])
print("Every alternate number:", num[::2])
First 3 numbers: [1, 2, 3]
Last 3 numbers: [8, 9, 10]
Every alternate number: [1, 3, 5, 7, 9]
Q3: Write a Python program to create an empty list. Add numbers 1, 2, and 3 using append().
Then extend the list with [4, 5, 6]. Print the final list.
In [57]: emp=[]
[Link](1)
[Link](2)
[Link](3)
print(emp)
print(emp+[4,5,6])
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
Q4: Write a Python program with a list of numbers [10, 20, 30, 40, 50].
Remove the number 30 using remove().
Remove the last element using pop().
Print the updated list after each step.
In [59]: lstNum=[10,20,30,40,50]
[Link](30)
print("remove 30: ",lstNum)
[Link]()
print("pop :",lstNum)
remove 30: [10, 20, 40, 50]
pop : [10, 20, 40]
Q5: Write a Python program to check if a given number is present in the list of numbers. If
present, print ”Found”, otherwise ”Not Found”
In [1]: num = [10, 20, 30, 40, 50, 60]
n = int(input("Enter a number to search: "))
if n in num:
print("Found")
else:
print("Not Found")
Found
Q6: Write a Python program to calculate the sum, average of numbers and also find the
maximum and minimum numbers stored in the list.
In [5]: list1=[1,2,3,4,5,6]
print("Sum of list items :",sum(list1),"avg: ",sum(list1)/len(list1))
print('maximun:',max(list1),'\nminimum:',min(list1))
Sum of list items : 21 avg: 3.5
maximun: 6
minimum: 1
Q7: Write a Python program that counts how many times a given number appears in the list.
In [6]: L=[1,2,3,1,3,4,2,2,1,4,5]
print("count of 2:",[Link](2))
count of 2: 3
Q8: Write a Python program to remove duplicates from the list [1, 2, 2, 3, 4, 4, 5] and print the
unique values.
In [7]: l= [1, 2, 2, 3, 4, 4, 5]
unique = []
for n in l:
if n not in unique:
[Link](n)
print("Unique values list:", unique)
Unique values list: [1, 2, 3, 4, 5]
Q9: Write a Python program to reverse a list using list slicing.
In [8]: lst=[1,2,3,4,5,6,7]
lst[::-1]
Out[8]: [7, 6, 5, 4, 3, 2, 1]
Q10:Concatenate two lists index-wise.
list1 = [“M”, “na”, “i”, “Ku”]
list2 = [“y”, “me”, “s”, “nal”]
Expected output: [’My’, ’name’, ’is’, ’Kunal’]
In [9]: list1 = ['M', 'na', 'i', 'Ku']
list2 = ['y', 'me', 's', 'nal']
print([a + b for a, b in zip(list1, list2)])
['My', 'name', 'is', 'Kunal']
Q11: Write a Python program using list comprehension to create a list of even numbers
between 1 and 20.
In [10]: evenNum = [x for x in range(1, 21) if x % 2 == 0]
print("Even numbers between 1 and 20:", evenNum)
Even numbers between 1 and 20: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Q12: Write a Python program to find the second largest number in a list of numbers.
In [11]: num= [10, 25, 7, 50, 32, 50]
unique= list(set(num))
[Link](reverse=True)
if len(unique) >= 2:
print("Second largest number is:", unique[1])
else:
print("List does not have a second largest number.")
Second largest number is: 32
Q13: Write a Python program to check if the list [1, 2, 3, 2, 1] is a palindrome (same forwards
and backwards).
In [12]: num = [1, 2, 3, 2, 1]
if num == num[::-1]:
print("The list is a palindrome")
else:
print("The list is not a palindrome")
The list is a palindrome
Q14: Given a two Python list. Iterate both lists simultaneously such that list1 should display item
in original order and list2 in reverse order
list1 = [20, 35, 45, 78]
list2 = [100, 200, 300, 400]
Expected output:
20 400
35 300
45 200
78 100
In [35]: list1 = [20, 35, 45, 78]
list2 = [100, 200, 300, 400]
for a, b in zip(list1, reversed(list2)):
print(a,b)
20 400
35 300
45 200
78 100
Q15: Add item 7000 after 6000 in the following Python List
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
Expected output:[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40
In [14]: list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
list1[2][2].append(7000)
print(list1)
[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
Q16: Given a Python list, find value 20 in the list, and if it is present, replace it with 200. Only
update the first occurrence of a value
list1 = [5, 10, 15, 20, 25, 50, 20]
Expected output: list1 = [5, 10, 15, 200, 25, 50, 20]
In [15]: list1 = [5, 10, 15, 20, 25, 50, 20]
if 20 in list1:
index = [Link](20)
list1[index] = 200
print("Updated list:", list1)
Updated list: [5, 10, 15, 200, 25, 50, 20]
Q17: Write a Python program to split a sentence ”Python is fun” into a list of words, and then
join them back into a single string separated by -.
In [19]: sentence = "Python is fun"
words = [Link]()
print("List of words:", words)
joined = "-".join(words)
print("Joined string:", joined)
List of words: ['Python', 'is', 'fun']
Joined string: Python-is-fun
Q18: Given a list of strings, display each string reversed.
List = [“cat”, “dog”, “bird”]
Expected output: [‘tac’, ‘god’, ‘drib’]
In [21]: List = ["cat", "dog", "bird"]
rev= [word[::-1] for word in List]
print(rev)
['tac', 'god', 'drib']
Q19: Given a nested list, flatten it into a single list.
Input: [[1, 2], [3, 4], [5, 6]]
Expected Output: [1, 2, 3, 4, 5, 6]
In [25]: list = [[1, 2], [3, 4], [5, 6]]
newList = [item for x in list for item in x]
print(newList)
[1, 2, 3, 4, 5, 6]
Q20: Given a list, find the index where the sum of the left part equals the sum of the right part.
Input: [1, 7, 3, 6, 5, 6]
Expected Output: 3 (because [1+7+3] = [5+6])
In [29]: nums = [1, 7, 3, 6, 5, 6]
for i in range(len(nums)):
if sum(nums[:i]) == sum(nums[i+1:]):
print(i)
break
Name:Santosh Dalei
Roll No.:26
SIC:23BCEA27
Branch:CEN-A2