List Special Operations in Python
We can perform concatenation using the + operator, multiplication/repetition using the * operator, and splitting elements of a list using slicing ([:]). These concepts are shown in the example below.
Example: List Concatenation, Multiplication & Slicing
a = [1,2,3,7,8,1]
del(a)
b = [4,5,6]
c = a + b
print(c)
c = a * 3
print(c)
print(a[1:3])
print(a[1:])
print(a[:3])
print(a[-3:-1])
Assignments of List
- Check if a key is present in every segment of size k in an array
- Find the minimum and maximum element in an array
- Write a program to reverse the array
- Write a program to sort the given array
- Find the Kth largest and Kth smallest number in an array
- Find the occurrence of an integer in the array
- Sort the array of 0s, 1s, and 2s
- Range and Coefficient of array
- Move all the negative elements to one side of the array
- Find the Union and Intersection of the two sorted arrays
Level 2 Assignments
- Write a program to cyclically rotate an array by one
- Find the missing integer
- Count pairs with given sum
- Find duplicates in an array
- Sort an array using the Quicksort algorithm
- Find common elements in three sorted arrays
- Find the first repeating element in an array
- Find the first non-repeating element in an array
- Find the largest three elements in an array
- Rearrange the array in alternating positive and negative items
- Find if there is any subarray with sum equal to zero
- Find the Largest Sum Contiguous Subarray
- Find factorial of a large number
- Find Maximum Product Subarray
- Find longest consecutive subsequence
- Find minimum element in a rotated sorted array
- Find all elements that appear more than N/K times
- GCD of given index ranges in an array
- Minimize the maximum difference between heights
- Minimum number of jumps to reach the end
- Find the two repetitive elements in a given array
- Find a triplet that sums to a given value
- Construct an N * M matrix from user input
- Find the row with the maximum number of 1’s
- Print the matrix in spiral order
- Find whether an array is a subset of another array
- Implement two stacks in an array
- Majority Element
- Wave Array
- Trapping Rainwater
Level 3 Assignments
- Maximum Index
- Max sum path in two arrays
- Find Missing and Repeating
- Stock Buy and Sell Problem
- Pair with given sum in a sorted array
- Chocolate Distribution Problem
- Longest Consecutive Subsequence
- Print all possible combinations of r elements in a given array
13 Comments
WAP to count total even number and Odd number in Tuple?
ReplyDeleteSol:-t=(1,2,3,4,5,6,7,8,9)
count1=0
count2=0
for i in t:
if i%2==0:
count1=count1+1
else:
count2=count2+1
print("count of even numbers:",count1)
print("count of odd numbers:",count2)
#Wap to find Negative Elements in list?
ReplyDeletelst=[-2,4,2,-8,5,-4,55,44,-7]
for i in range(0,len(lst)):
for j in range(i,len(lst)):
if lst[i]==lst[i] and lst[i]<0:
print("Negative Elements",lst[j])
break
#Wap Program to reverse the list?
ReplyDeletelst=[3,4,2,5,65]
for i in range(len(lst)-1,-1,-1):
print(lst[i],end=' ')
#WAP to Find max and min Element in list?
ReplyDeletelst=[44,22,11,55,44]
maxi=lst[0]
for i in range(0,len(lst)):
if maxilst[i]:
minn=lst[i]
print("Minimum Element is ",minn)
#WAP to Sort the list using sort() method ?
ReplyDeletelst=[6,4,7,8,2]
lst.sort()
print(lst)
This comment has been removed by the author.
ReplyDelete#Find whether an List is a subset of another List?
ReplyDeletelst=[10,4,5,7,2]
lst1=[2,7,4]
for i in lst:
if i in lst1:
print("List1 is Subset of List",lst,lst1)
break
#sorting a ist
ReplyDeletex=[12,25,85,32,14]
for i in range (0,len(x)):
for j in range(i+1,len(x)):
if x[i]>x[j]:
x[i],x[j]=x[j],x[i]
print(x)
#sorting decreasing order:
x=[12,25,85,32,14]
for i in range (0,len(x)):
for j in range(i+1,len(x)):
if x[i]<x[j]:
x[i],x[j]=x[j],x[i]
print(x)
#duplicate element remove
ReplyDeletedef Remove(duplicate):
final_list=[]
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list
duplicate=[2,4,10,20,5,10,8]
print(Remove(duplicate))
#another mathod
l=[2,4,10,20,5,10,8]
a=list(set(l))
print(a)
#find out negative element in the list
ReplyDeletelist=[-1,2,5,-3,55,-26,27]
for i in range (0,len(list)):
for j in range(i,len(list)):
if list[i]==list[j]and list[i]<0:
print("negative element",list[j])
break
#subset of list
ReplyDeletel1=[1,2,4,5,8]
l2=[2,5,9,6,4,10,12,25]
for i in l1:
if i in l2:
print("l2 is subset of l1",l1,l2)
break
#counting even or odd element in tuple
ReplyDeletet=(1,2,3,4,5,7,8,9,)
count1=0
count2=0
for i in t:
if i%2==0:
count1=count1+1
else:
count2=count2+1
print("count of even number",count1)
print("count of even number", count2)
#square of list element
ReplyDeletex = [4, 5, 6, 7, 11, 12]
y = [i ** 2 for i in x]
print(y)
# 2nd method
def square(list):
ret = []
for i in list:
ret.append(i ** 2)
return ret
# 3rd method:
def square(list):
for i in list:
yield i**2
POST Answer of Questions and ASK to Doubt