List within list is called nested list true or false

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.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.

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

Video liên quan

Postingan terbaru

LIHAT SEMUA