InterviewSolution
| 1. |
What are the different ways to access the elements of a list. Give example? |
|
Answer» List is constructed by placing expressions within square brackets separated by commas. Example for List is [10, 20]. The elements of a list can be accessed in two ways. The first way is via our familiar method of multiple assignment, which unpacks a list into its elements and binds each element to a different name. 1st: = [10, 20] x, y: = 1st In the above example x will become 10 and y will become 20. A second method for accessing the elements in a list is by the element selection operator, also expressed using square brackets. Unlike a list literal, a square – brackets expression directly following another expression does not evaluate to a list value, but instead selects an element from the value of the preceding expression. 1st [0] 10 1st [1] 20 |
|