Python - Lists

Why should I learn to solve Python: Lists technical interview questions?

Learn and practise solving Python: Lists technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.

Where can I get technical Python: Lists technical interview questions and answers with explanations?

IndiaBIX provides you with lots of fully solved Python: Lists technical interview questions and answers with a short answer description. You can download Python: Lists technical interview questions and answers as PDF files or e-books.

How do I answer Python: Lists technical interview questions from various companies?

You can answer all kinds of Python: Lists technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Lists technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.

1.
What is a list in Python?

In Python, a list is a mutable, ordered collection of elements. Each element can be of any data type, and the elements are enclosed within square brackets []. Lists allow for easy storage and manipulation of data.

Here's an example program that demonstrates the basic usage of lists:

# Creating a list
my_list = [1, 2, 3, 'four', 5.0]

# Accessing elements in a list
print(my_list[0])  # Output: 1
print(my_list[3])  # Output: 'four'

# Modifying elements in a list
my_list[1] = 'two'
print(my_list)  # Output: [1, 'two', 3, 'four', 5.0]

# Adding elements to a list
my_list.append(6)
print(my_list)  # Output: [1, 'two', 3, 'four', 5.0, 6]

# Removing elements from a list
my_list.remove('four')
print(my_list)  # Output: [1, 'two', 3, 5.0, 6]

# List slicing
subset = my_list[1:4]
print(subset)  # Output: ['two', 3, 5.0]

The above program initializes a list, accesses and modifies its elements, adds new elements, removes an element, and demonstrates list slicing.

Outputs:

1
'four'
[1, 'two', 3, 'four', 5.0]
[1, 'two', 3, 'four', 5.0, 6]
[1, 'two', 3, 5.0, 6]
['two', 3, 5.0]

2.
How do you create an empty list in Python?

To create an empty list in Python, you can use the square brackets with no elements between them. Here's an example program demonstrating how to create an empty list:

# Creating an empty list
empty_list = []

# Checking the type and content of the list
print(type(empty_list))  # Output: <class 'list'>
print(empty_list)  # Output: []

The program initializes an empty list and then uses the type() function to confirm that it is indeed a list. Finally, it prints the empty list.

Output:

<class 'list'>
[]

3.
Explain the difference between lists and tuples in Python.

Lists and tuples are both data structures in Python, but they have some key differences. Here's an explanation along with an example program:

Lists:

A list is a mutable, ordered collection of elements. Mutable means that you can change the elements of a list after it is created. Lists are defined using square brackets [].

# Creating a list
my_list = [1, 2, 3, 'four', 5.0]

# Modifying elements in a list
my_list[1] = 'two'
print(my_list)  # Output: [1, 'two', 3, 'four', 5.0]

Tuples:

A tuple is an immutable, ordered collection of elements. Immutable means that once a tuple is created, you cannot modify its elements. Tuples are defined using parentheses ( ).

# Creating a tuple
my_tuple = (1, 2, 3, 'four', 5.0)

# Attempting to modify elements in a tuple (will result in an error)
# my_tuple[1] = 'two'  # Uncommenting this line will raise a TypeError

The key difference is that lists are mutable, allowing for modification of elements, while tuples are immutable, preventing any changes after creation.

Outputs:

[1, 'two', 3, 'four', 5.0]

4.
How do you access elements in a list using indexing?

In Python, elements in a list are accessed using indexing. Indexing starts from 0 for the first element and goes up to len(list) - 1 for the last element. Here's an example program demonstrating how to access elements in a list:

# Creating a list
my_list = [1, 2, 3, 'four', 5.0]

# Accessing elements using positive indexing
first_element = my_list[0]
third_element = my_list[2]
last_element = my_list[-1]

print(first_element)  # Output: 1
print(third_element)  # Output: 3
print(last_element)   # Output: 5.0

# Accessing elements using negative indexing
second_to_last_element = my_list[-2]
print(second_to_last_element)  # Output: four

The program initializes a list and demonstrates accessing elements using both positive and negative indexing. Positive indexing starts from the beginning of the list, while negative indexing starts from the end.

Outputs:

1
3
5.0
four