Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Nested List

A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list.

You can use them to arrange data into hierarchical structures.

Creating a Nested List

my_list = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

Access Nested List

You can access individual items in a nested list using multiple indexes.

my_list = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']
print(L[2])  
# Prints ['cc', 'dd', ['eee', 'fff']]  

print(L[2][2])  
# Prints ['eee', 'fff']  

print(L[2][2][0])  
# Prints 'eee'

Add items

To add new values to the end of the nested list, use append() method.

When you want to insert an item at a specific position in a nested list, use insert() method.

You can merge one list into another by using extend() method.

my_list = ['a', ['bb', 'cc'], 'd']
my_list[1].append('xx')
print(my_list)
# Prints ['a', ['bb', 'cc', 'xx'], 'd']

my_list = ['a', ['bb', 'cc'], 'd']
my_list[1].insert(0,'xx')
print(my_list)
# Prints ['a', ['xx', 'bb', 'cc'], 'd']

my_list = ['a', ['bb', 'cc'], 'd']
my_list[1].extend([1,2,3])
print(my_list)
# Prints ['a', ['bb', 'cc', 1, 2, 3], 'd']

Remove items

If you know the index of the item you want, you can use pop() method. It modifies the list and returns the removed item.

If you’re not sure where the item is in the list, use remove() method to delete it by value.

my_list = ['a', ['bb', 'cc', 'dd'], 'e']
x = my_list[1].pop(1)
print(my_list)
# Prints ['a', ['bb', 'dd'], 'e']

# removed item
print(x)
# Prints cc

my_list = ['a', ['bb', 'cc', 'dd'], 'e']
my_list[1].remove('cc')
print(my_list)
# Prints ['a', ['bb', 'dd'], 'e']

Try the following example in the editor below.

You are given a nested list named ‘my_list’, perform the operations as defined in the comments.

NOTE: You don’t need to change/remove any code. Only add the line of code, wherever needed.

Start solving Nested List on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test