KnowledgeBoat Logo
|

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
  1. y.extend(x) modifies the list y by adding the elements of x to the end of y. So, y becomes [30, 40, 2, 3].
  2. z.append(x) adds the entire list x as a single element to the end of z. So, z becomes [88, 99, [2, 3]].

Answered By

1 Like


Related Questions