Python Lists

In Python, lists(consider list as array in C/C++ except it can hold different data types, whereas in C/C++ arrays can hold values of same data types) are a fundamental data type used to store a collection of items. They are written using square brackets and separated by commas.

1list_literal = [1, 2, 3, 4, 5]

Lists are mutable and ordered elements, which means that their values can be changed after they are created. This allows you to add, remove, or modify elements in a list.

1numbers = [1, 2, 3, 3, 4, 5]

add new value to the list

1numbers.append(6) # add an element to the list

remove last value frome list

1numbers.pop() # remove last element from the list

remove a value frome list, this will only remove the first occurance of the value in the list

1numbers.remove(3) # remove an element from the list

update a value in list using index position, this will update value at index 1

1numbers[1] = 10 # modify an element in the list

List with different data types

list variable data holds three different data types, as you can see in the for loop print statement

1data = [1, "Brenda", 5.5] 2for i in data: 3 print(i, type(i)) 4 5Output: 61 <class 'int'> 7Brenda <class 'str'> 85.5 <class 'float'>

update a value in a list to different data type value

1data = [1, "Brenda", 5.5] 2data[2] = "five" 3print(data) # Output: [1, 'Brenda', 'five']

Nested List

List can hold data type of its own, below code is used to create nested list using for loop with range

1nested_list = [] 2 3for i in range(0,3): 4 nested_list.append([]) 5 for j in range(0,5): 6 nested_list[i].append(j) 7 8print(nested_list) 9 10Output: 11[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

List comprehension

you can use list comprehension to create a new list by applying an operation to each element of an existing list.

1numbers = [1, 2, 3, 4, 5] 2results = [x+x for x in numbers] 3print(results) # Output: [2, 4, 6, 8, 10]

same operation without list comprehension

1def add(x): 2 return x+x 3 4numbers = [1, 2, 3, 4, 5] 5results = [] 6for i in numbers: 7 results.append(add(i)) 8print(results) # Output: [2, 4, 6, 8, 10]

List Indexing

Elements of the list can be access using the indexing method, index of the list always start with 0.

To access the first element of the list, index 0 is used. To access the last element of the list, use index value list_length-1 (should be length of list minus 1)

1numbers = [1, 2, 3, 4, 5] 2 3print(numbers[0]) 4print(numbers[1]) 5print(numbers[2]) 6print(numbers[3]) 7print(numbers[4]) 8 9Output: 101 112 123 134 145

List Negative Indexing

Python list provides a way to access list elements using negative index

To access the last element of the list, use -1. To access the first element of the list, use -(list_len).

1print(numbers[-1]) 2print(numbers[-2]) 3print(numbers[-3]) 4print(numbers[-4]) 5print(numbers[-5]) 6 7Output: 85 94 103 112 121

List Splitting

List slice is a technique to access the element inside list in a range

1list_vales[start_index:end_index:steps]

start_index: value will start from this end_index: value range will end one step before this index value steps: steps to skip over the values in the list

1numbers = [1, 2, 3, 4, 5] 2 3# these expression will return same results 4print(numbers[:]) 5print(numbers[0:]) 6print(numbers[0:5]) 7print(numbers[:5]) 8 9Output: 10[1, 2, 3, 4, 5] 11[1, 2, 3, 4, 5] 12[1, 2, 3, 4, 5] 13[1, 2, 3, 4, 5]

Note: If start or end index is not specified, it will take the value from the start or till end

1 2# steps in slicing 3print(numbers[0:5:2]) # Output: [1, 3, 5] 4print(numbers[::2]) # Output: [1, 3, 5] 5print(numbers[::3]) # Output: [1, 4] 6 7# from index 1 to 3 8print(numbers[1:4]) # Output: [2, 3, 4]

List negative indexing

1numbers = [1, 2, 3, 4, 5] 2print(numbers[-5:]) # Output: [1, 2, 3, 4, 5] 3print(numbers[-5:-2]) # Output: [1, 2, 3] 4print(numbers[:-1]) # Output: [1, 2, 3, 4]

Reverse a list

1numbers = [1, 2, 3, 4, 5] 2print(numbers[::-1]) # Output: [5, 4, 3, 2, 1]