Informatics Practices
The data type list is an ordered sequence which is mutable and made up of one or more elements. Unlike a string which consists of only characters, a list can have elements of different data types such as integer, float, string, tuple or even another list. A list is very useful to group elements of mixed data types. Elements of a list are enclosed in square brackets and are separated by comma.
(i) How is a list different from a dictionary?
(ii) Find the values of y and z after execution:
x=[2, 3]
y=[30, 40]
z=[88, 99]
y.extend(x)
z.append(x)
print (y)
print (z)
Python List Manipulation
2 Likes
Answer
(i) A list in Python is an ordered collection of elements that can be accessed by their index. In contrast, a dictionary is an unordered collection of key-value pairs, where values are accessed using unique keys. Elements of a list are enclosed in square brackets and are separated by comma while dictionaries are enclosed in curly brackets, with keys and values separated by colons and key-value pairs separated by commas.
(ii)
Output
[30, 40, 2, 3]
[88, 99, [2, 3]]
Explanation
y.extend(x)
modifies the listy
by adding the elements ofx
to the end ofy
. So,y
becomes [30, 40, 2, 3].z.append(x)
adds the entire listx
as a single element to the end ofz
. So,z
becomes [88, 99, [2, 3]].
Answered By
1 Like
Related Questions
Write the output for the following print statements in Python.
Sub_Teacher = {"English":"Mr. Gill", "Maths": "Mr. R.N. Pandey", "IP":"Ms. Renu Ahuja", "Physics": "Ms. Meenu Lal", "Chemistry":"Ms. Mamta Goel"} print(Sub_Teacher['Chemistry']) #Line1 print(Sub_Teacher.keys()) #Line2 print(len(Sub_Teacher)) #Line3
Rohit, a Librarian has created a Table Book with the following fields:
Bookid, Bookname, Author and Price. He has entered 20 records in the table.
Which command should he use to do the following:
(i) To change the price of Computer Science book from 450 to 500.
(ii) To insert Primary Key in the table.
(iii) What is the difference between delete and drop command.
Write a program in Python to input elements in an empty list. Also delete an element from the list which a user will input.
Consider the following list.
emp=["Aditya", 40000, "Deepak",50000, "Yashmit", 60000, "Bhavya", 80000]
(i) To Insert the value "Ramit" at index number 4.
(ii) To check whether element 50000 is present in the list or not.
(iii) To display the first 3 elements from the list.
(iv) To remove the fifth element from the list.