Python List
- Categories Python, Python List, Python
Q. 1 What is list in python? Also give an example.
Ans.
- A list is an ordered collection of items separated by commas and enclosed within square brackets ([]).
- It can contain data of any type.
- List is mutable, means we can change any data inside the list unlike string.
- An empty list is denoted by empty square brackets[]
- g. list = [ ‘abcd’, 26 , ‘j’, 3.14 ]
Q. 2 How to create a python list?
Ans.
- To create a new Python list, you can either assign empty square brackets L=[ ] or you can use the list constructor L= list().
- You can also assign any values directly inside square brackets separated by comma.
L1= L = [ val1, val2,val3, val4….] - You can also create nested lists.
L2 = [val1, val2, [val31, val32], val4] - You can create lists from other sequences such as string or tuple.
L1 = list(“String”)
L2 = list( (1,2,3,4)) - Do note that creating a new list by inserting a string in square brackets, behaves differently than using a constructor which takes in a string.
Q. 3 What is the difference between creating two lists like this L1 = [‘Simply’] and L1 = list(‘Simply’)?
Ans. In the first option a list will be created and it will have only 1 element “Simply”. In second one a list will be created from string “Simply” and each individual character will be separate element of the list. So it will give output as [‘S’,’i’, ‘m’, ‘p’, ‘l’ , ‘y’ ]
Q. 4 Give 2 similarities and dis-similarities between lists and Strings.
Ans.
Similarities
- Indexing and Slicing work the same
- In, not in + and * operation works the same
Dis-Similarities
- Strings are non-mutable, lists are mutable
- Each character is stored directly in memory in String. In list each list element is stored as a reference (its address is stored)
Q.5 What is the output when following operators are applied on these list?
a = [ ‘Simply’, 2 ]
b = [ ‘Coding’, 3.14]
- print (a+b)
- print (a*2)
- print (2 in a)
- print (‘S’ in a)
- ‘C’ not in b
- 3 not in b
Ans.
- [ ‘Simply’, 2, ‘Coding’, 3.14]
- [ ‘Simply’, 2, ‘Simply’, 2]
- True
- False
- True
- True
Q. 6 Consider following 2 lists, what is the output of following List indexing and Slicing?
a = [ ‘Simply’, 2, 4, 10 ]
b = [ ‘Coding’, 3.14, 55, 14]
- print (a[0], b[0])
- a[-1]
- a [10]
- a[ :-1]
- a[0: ]
- a[:2] + a[2:]
- a[0:10]
Ans.
- Simply Coding
- 10
- Error
- [‘Simply’, 2, 4]
- [ ‘Simply’, 2, 4, 10 ]
- [ ‘Simply’, 2, 4, 10 ]
- [ ‘Simply’, 2, 4, 10 ]
Q. 7 What is the output of List editing commands on following lists?
a = [ ‘Simply’, 2, 4, 10 ]
b = [ ‘Coding’, 3.14, 55, 14]
- a[0:2] = [0,1]
- a[0:2] = [40]
- a[1] = 20
- del a[0]
- a[0: ]
- a.pop()
- a.pop(1)
- a.remove(2)
- a.clear()
- a.insert(2,40)
- a.append(2)
- a.extend([3,4])
Ans.
- [ 0, 1, 4, 10 ]
- [ 40, 4, 10 ]
- [ ‘Simply’, 20, 4, 10 ]
- [ 2, 4, 10 ]
- [ 4, 10 ]
- [ ‘Simply’, 2, 4 ]
- [ ‘Simply’, 4, 10 ]
- [ ‘Simply’, 4, 10 ]
- []
- [ ‘Simply’, 2, 40, 4, 10]
- [ ‘Simply’, 2, 4, 10, 2 ]
- [ ‘Simply’, 2, 4, 10, 3, 4 ]
Q. 8 List method ___ is used to add element at the end of the list.
- insert()
- append()
- extend()
- pop()
Ans. b
Q. 9 extend() Method use to add a ___ to the end of the list
- List
- Number
- String
- None of above.
Ans. a
Q. 10 Which List Method is used to add element at exact position? Also give Syntax for
Ans. Insert to add a element at specified position.
Syntax: list.insert(index, elmnt)
elmnt can be anything like string, number, list, etc.
Q. 11 Pop Function is used to ____ element at specific position.
- Remove
- Add
- Copy
- None of above
Ans. a
Q. 12 _____ List function is used to display elements in ascending or descending order.
Ans. sort()
Q. 13 What is the difference between second and third statement below?
L1 = [2,3]
L2 = L1
L3 = L1.copy()
Ans. The second statement does not creates a copy of L1. IT just assigns another name to L1. So if you make change in L1 it reflects in L2 too.
However third statement makes a true copy of L1. If L1 is changed, it does not impact L3.
Q. 14 Consider the following List and give the output after evaluating the code.
Lst = ['These',"are","a",["few","words"],"that","we","will","use"]
Lst[3:4][0]
[L[1]] + L[3]
remove('a')
print(Lst)
L2= ["simply",'Coding']
Lst.append(L2)
print(Lst)
Ans.
[‘few’, ‘words’]
[‘are’, ‘few’, ‘words’]
[‘These’, ‘are’, [‘few’, ‘words’], ‘that’, ‘we’, ‘will’, ‘use’]
[‘These’, ‘are’, ‘a’, [‘few’, ‘words’], ‘that’, ‘we’, ‘will’, ‘use’, [‘simply’, ‘Coding’]]
Q. 15 Give the output of following code.
a,b,c = [1,2],[1,2],[1,2]
print (a == b)
print (a is b)
d = a
print (a is d)
Ans. output.
True
False
True
Q. 16 Write a Python program to remove duplicates from a list.
Ans.
a = [10,20,30,20,10,50,60,40,80,50,40]
dup_items = set()
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)
Q.17 Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
Ans.
values = input("Input some comma separated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)
Q. 18 Write a Python program to create a histogram from a given list of integers.
Ans.
def histogram( items ):
for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)
histogram([2, 3, 6, 5])
Q. 19 Write a Python program to filter the positive numbers from a list.
Ans.
nums = [34, 1, 0, -23]
print("Original numbers in the list: ",nums)
new_nums = list(filter(lambda x: x >0, nums))
print("Positive numbers in the list: ",new_nums)
Q. 20 Write a Python program to remove and print every third number from a list of numbers until the list becomes empty.
Ans.
def remove_nums(int_list):
#list starts with 0 index
position = 3 - 1
idx = 0
len_list = (len(int_list))
while len_list>0:
idx = (position+idx)%len_list
print(int_list.pop(idx))
len_list -= 1
nums = [10,20,30,40,50,60,70,80,90]
remove_nums(nums)
Some List methods :
Method | Description |
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |