A list embedded in another list is called as list

Home/ Indian/Computer Science/3. Alist embedded in another list is called as Nested list

4. The links to another pages or document are called​

What is Python 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.

11. Lists¶

A list is an ordered collection of values. The values that make up a list are called its elements, or its items. We will use the term element or item to mean the same thing. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can be of any type. Lists and strings — and other collections that maintain the order of their items — are called sequences.

10 Lists

Contents

  1. Introduction to lists
  2. Unordered lists (UL), ordered lists (OL), and list items (LI)
  3. Definition lists: the DL, DT, and DD elements
    1. Visual rendering of lists
  4. The DIR and MENU elements

10.24. Nested Lists¶

A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list. If we print(nested[3]), we get [10, 20]. To extract an element from the nested list, we can proceed in two steps. First, extract the nested list, then extract the item of interest. It is also possible to combine those steps using bracket operators that evaluate from left to right.

Check your understanding

    list-23-2: What is printed by the following statements?

    alist = [ [4, [True, False], 6, 8], [888, 999] ] if alist[0][1][0]: print(alist[1][0]) else: print(alist[1][1])

  • 6
  • 6 is in the wrong list. alist[1] refers to the second item in alist, namely [888,999].
  • 8
  • 8 is in the wrong list. alist[1] refers to the second item in alist, namely [888,999].
  • 888
  • Yes, alist[0][1][0] is True and alist[1] is the second list, the first item is 888.
  • 999
  • alist[0][1][0] is True. Take another look at the if statement.

What’s a List of Lists?

Definition: A list of lists in Python is a list object where each list element is a list by itself. Create a list of list in Python by using the square bracket notation to create a nested list [[1, 2, 3], [4, 5, 6], [7, 8, 9]].

A list embedded in another list is called as list

Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!

A list embedded in another list is called as list

Memory Analysis

It’s important that you understand that a list is only a series of references to memory locations. By playing with the code visualizer, you’ll gain a deeper understanding of how Python works at its core:

Simply click the “Next” button to see how each line of code unfolds.

Create a List of Lists in Python

Create a list of lists by using the square bracket notation. For example, to create a list of lists of integer values, use [[1, 2], [3, 4]]. Each list element of the outer list is a nested list itself.

Convert List of Lists to One List

Say, you want to convert a list of lists [[1, 2], [3, 4]] into a single list [1, 2, 3, 4]. How to achieve this? There are different options:

  • List comprehension [x for l in lst for x in l] assuming you have a list of lists lst.
  • Unpacking [*lst[0], *lst[1]] assuming you have a list of two lists lst.
  • Using the extend() method of Python lists to extend all lists in the list of lists.

Find examples of all three methods in the following code snippet:

lst = [[1, 2], [3, 4]] # Method 1: List Comprehension flat_1 = [x for l in lst for x in l] # Method 2: Unpacking flat_2 = [*lst[0], *lst[1]] # Method 3: Extend Method flat_3 = [] for l in lst: flat_3.extend(l) ## Check results: print(flat_1) # [1, 2, 3, 4] print(flat_2) # [1, 2, 3, 4] print(flat_3) # [1, 2, 3, 4]

Due its simplicity and efficiency, the first list comprehension method is superior to the other two methods.