Lists are mutable ordered collections of elements enclosed in square brackets. Elements within lists can be accessed by their index, modified by changing values or inserting/appending new elements. Lists also allow reordering elements through methods like reverse, sort, and insert which can rearrange the order of elements in the list.
Lists are mutable ordered collections of elements enclosed in square brackets. Elements within lists can be accessed by their index, modified by changing values or inserting/appending new elements. Lists also allow reordering elements through methods like reverse, sort, and insert which can rearrange the order of elements in the list.
Lists are mutable ordered collections of elements enclosed in square brackets. Elements within lists can be accessed by their index, modified by changing values or inserting/appending new elements. Lists also allow reordering elements through methods like reverse, sort, and insert which can rearrange the order of elements in the list.
l1[1] // 'a' - sequence of element l1[1:4] // ['a', true, 4.5]
=> Modifying a list
- changing the element at 3rd index l1[3] = 100 // [1, 'a', true, 100, 'hello'] - Appending a new element (adding) at last l1.append('Tcil') // [1, 'a', true, 100, 'hello', 'Tcil'] - popping the last element myvalue=l1.pop() // it will pop out the last element [1, 'a', true, 100, 'hello'] => Modifying a list - Reversing element of list l1 = [1, 'a', 2, 'b', 3, 'c'] l1.reverse() ['c', 3, 'b', 2, 'a', 1] inserting element at a specified index l1.insert(1, 'testing') [1, 'testing', 'a', 2, 'b', 3, 'c'] sorting a list l1= ["grapes", "apple", "mango", "banana"] l1.sort() [ "apple", "banana", "grapes", "mango"]