Python-List-IQ

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[2] = 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


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[3] = 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[1]=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.

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

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

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

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

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.

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

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.