Python Lists

Python Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java).
In simple language, a list is a collection of things, enclosed in [ ] and separated by commas.
Lists are the simplest containers that are an integral part of the Python language.
Lists need not be homogeneous always which makes it the most powerful tool in Python.
A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Example:

a = ["hello", 12, "world", 4, [1, 2]]
print(a)
Creating a List in Python

Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesn’t need a built-in function for its creation of a list.
Example:

a = ["hello rguktians",123]
print(a)
print(type(a))
>List Items

A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.
List items can be of any data type:

a = [1,5,3,5,2,1,3]
print(a) ## List with duplicate values
b = ["a",1,"rguktweb","xyz",14j,18.55]
print(b) ## Mixed type of values
c = [111,222,"abc",[],[111,222,"hello"],("hello world","")]
print(c) ## list with nested list ,tuple...
Accessing elements from the List

In order to access the list items refer to the index number. Use the index operator [] to access an item in a list. The index must be an integer. Nested lists are accessed using nested indexing.
List items are indexed, the first item has index [0], the second item has index [1] etc
Example:

x = [1,2,3,4,5,6]
print(x[1]) ## 2
print(x[5]) ## 6
Negative indexing

In Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3].
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc.
Example:

x = [1,2,3,4,5,6]
print(x[-1]) ## 6
print(x[-3]) ## 4
List Length

Python len() is used to get the length of the list.
Example:

a = [11,22,33,44,55,66,77]
print(len(a))
Input of a List

We can take the input of a list of elements as string, integer, float, etc. But the default one is a string.
Here we use split() method.
Example:

a = "this is a string"
b = a.split() ## split at white spaces
print(b) ## ['this','is','a','string']
Adding Elements to a List

Elements can be added to the List by using the built-in append() function.
Only one element at a time can be added to the list by using the append() method, for the addition of multiple elements with the append() method, loops are used.
Tuples can also be added to the list with the use of the append method because tuples are immutable.
Unlike Sets, Lists can also be added to the existing list with the use of the append() method.
When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
insert() and extent() methods are also used to add elements to a list
Example:

a = int(input("Enter any Number :"))
b = input("Enter any string :")
l = [321,"strings","lists"]
l.append(a)
print(l)
l.append(b)
print(l)
Changing Elements of a List

To change the value of a specific item, refer to the index number
To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values:
Example:

listt = ['r','g','u','k','t','w','e','b']
listt[0] = "R"
print(listt)
listt[1:4] = "G","U","K"
print(listt)
listt[4:5] = "T","S"
print(listt)

Note : The length of the list will change when the number of items inserted does not match the number of items replaced.

The list() Constructor

It is also possible to use the list() constructor when creating a new list.
Example:

newlist = list((1,2,3,4,5,6,7,8,9,10)) 
print(newlist) ## note the double round brackets
List Comprehension

Python List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc.
A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.
Example:

newlist = [i for i in "rguktweb"]
print(newlist) ## ['r','g','u','k','t','w','e','b']
new = [x**2 for x in range(10)]
print(new) ## Usind loop
new1 = [z for z in range(15) if z!=3 and z!=4]
print(new1) ## Using loop with condition