What is a List? List is a data type in Python. List is used to store multiple values ( it can string, integer, float and other lists also ) in a single variable. Lists are mutable, which means you can modify, add, and remove elements from them. We can define a list in the following ways, 1. myList = [value1, value2, value3] 2. myTuple = (value1, value2, value3) myTupleList = list(myTuple) 3. myStr = 'My String' myStrList = list(myStr) Example: a = [1,2,3] print(a) Output: [1,2,3] You can access individual elements in a list using square brackets and the index of the element. Indexing starts from 0 in the Python List. Like we want to access the third element of the above example list, we can write, a[2] Output: 3 If we want a string in a list, we will use double quotes for the element. Also if we want to use a list in the list we can use it. Example: myList = [2,4, ["a", 4]] myList[2] Output: ["a", 4] List can have duplicate elements. We can add, remove
Small & Important