Nested Lists in Python

What is the output of this code snippet?

 

a = ['a', 'b']
n = [1, 2]
x = [a, n]
print(x[1])

 

Many practical code projects use lists containing not only primitive but also complex data types. Examples of primitive data types are integers, strings, or floats. Examples of complex data types are customer objects, camera events, or even lists.

In the puzzle, we show the latter: there is a nested list that contains two other lists. Accessing an element of this list using the index notation returns a list itself.

Python is a dynamically typed programming language. Hence, there can be hybrid lists containing different data types. Yet, this is not too common.


Are you a master coder?
Test your skills now!

 

Related Video

Solution

[1,2]

 

Leave a Comment

Your email address will not be published. Required fields are marked *