0% found this document useful (0 votes)
37 views

Basic nested list ip

Basic nestes list ip Impppppp

Uploaded by

alvira5090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Basic nested list ip

Basic nestes list ip Impppppp

Uploaded by

alvira5090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic nested list

L = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Nested list with different lengths of inner lists


L = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]

# Nested list with mixed data types


L = [["Alice", 30], ["Bob", 25], ["Charlie", 35]]

# Deeper nested lists


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

Accessing Elements in a Nested List


To access elements in a nested list, you use multiple indices – one for each level of the
list. The first index specifies which of the inner lists to access, and the subsequent indices
specify the element within that inner list.

The indexes for the elements in a nested list are illustrated as below:
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

print(L[2]) # Output: ['cc', 'dd', ['eee', 'fff']]


print(L[2][2]) # Output: ['eee', 'fff']
print(L[2][2][0]) # Output: eee

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

print(L[-3]) # Output: ['cc', 'dd', ['eee', 'fff']]


print(L[-3][-1]) # Output: ['eee', 'fff']
print(L[-3][-1][-2]) # Output: eee
o add a new element to the end of a nested list, you can use the append() method.

L = ['a', ['bb', 'cc'], 'd']


L[1].append('xx')
print(L)
# Output: ['a', ['bb', 'cc', 'xx'], 'd']

If you need to insert an element at a specific position within a nested list,


the insert() method is the right tool.

L = ['a', ['bb', 'cc'], 'd']


L[1].insert(0,'xx')
print(L)
# Output: ['a', ['xx', 'bb', 'cc'], 'd']

Finally, the extend() method allows you to merge one list into another.

L = ['a', ['bb', 'cc'], 'd']


L[1].extend([1, 2, 3])
print(L)
# Output: ['a', ['bb', 'cc', 1, 2, 3], 'd']
Removing Elements from a Nested List
There are different ways to remove elements from a nested list depending on your
specific needs.

If you know the index of the element you want to remove, the pop() method is a good
choice. This method not only removes the element at the specified index but also
returns its value.

L = ['a', ['bb', 'cc', 'dd'], 'e']


x = L[1].pop(1)
print(L)
# Output: ['a', ['bb', 'dd'], 'e']

# removed element
print(x)
# Output: cc

In cases where you don’t need the removed value, the del statement is a simple and
efficient way to delete an element by its index.

L = ['a', ['bb', 'cc', 'dd'], 'e']


del L[1][1]
print(L)
# Output: ['a', ['bb', 'dd'], 'e']

If you don’t know the exact index but know the value of the element you want to
remove, the remove() method is the way to go. This method searches the list and
removes the first occurrence of the specified value.

L = ['a', ['bb', 'cc', 'dd'], 'e']


L[1].remove('cc')
print(L)
# Output: ['a', ['bb', 'dd'], 'e']
Finding the Length of a Nested List
Determining the number of elements within a nested list is simple using Python’s built-
in len() function. This function returns the length of a list, which is the number of
elements it contains.

L = ['a', ['bb', 'cc'], 'd']

print(len(L)) # Output: 3
print(len(L[1])) # Output: 2

You might also like