Python Lists
A list is a value which contains multiple values in an ordered sequence. In a string the values are characters but in a list the values can be at any type. A list starts with an opening square bracket and ends with a closing square bracket. Values inside the list are also called items. Items are separated with commas. For example:
list1 = ["c", "python", "java", "javascript"]
list2 = [5, 6, 7, 8]
Just the string values are typed with quote characters to mark where the string begins and ends. Like string the list indices also start with 0. The items of a list don’t have to be the same type. A list can contain a string, a float, an integer and other data types also. For example:
list1 = ["python", 7, 3.75, True, None]
Accessing List Items
You can access the list items by referring to the index number.
Example:
list1 = ["python", 7, 3.75, True, None]
print(list1[0])
print(list1[3])
Output:
python True
Just an index can get a single value from a list but by specifyling a range you can get several values from a list.
Example:
list1 = ["python", 7, 3.75, True, None]
print(list1[0:3])
print(list1[1:4])
Output:
['python', 7, 3.75] [7, 3.75, True]
You can also ignore one or both of the indices on either side of the colon. Ignore the first side is just same as using 0 and ignore the right side will give the value to the end of the list.
Example:
list1 = ["python", 7, 3.75, True, None]
print(list1[:3])
print(list1[1:])
Output:
['python', 7, 3.75] [7, 3.75, True, None]
Changing Values in a List
You can change a specific item use an index of a list.
Example:
list1 = ["python", 7, 3.75, True, None]
list1[0] = "programming"
print(list1)
Output:
['programming', 7, 3.75, True, None]
List Concatenation
There are several way in python to concatenate multiple lists. The easiest way is to using the + operator. It combines two list into a new list.
Example:
list1 = ["c", "python", "java"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Output:
['c', 'python', 'java', 1, 2, 3]
You can also replicate a list using * operator with a list and an integer value.
Example:
list1 = ["c", "python", "java"]
list2 = list1 * 3
print(list2)
Output:
['c', 'python', 'java', 'c', 'python', 'java', 'c', 'python', 'java']
Removing List Items
You can remove a list item using the del statement.
Example:
list1 = ["c", "python", "java"]
del list1[2]
print(list1)
Output:
['c', 'python']
You can also remove the list completely using the del statement.
Example:
list1 = ["c", "python", "java"]
del list1
print(list1)
Output:
Traceback (most recent call last): File "C:\Users\Hp\Desktop\py\python.py", line 3, in <module> print(list1) NameError: name 'list1' is not defined
Note: Since the total tuple is deleted an error is raised in the output.
Length of a List
You can define the length of a list using the len() method.
Example:
list1 = ["c", "python", "java"]
print(len(list1))
Output:
3
List Methods
There are a set of built-in methods in python which you can use on lists.
Method | Description |
---|---|
append() | Inserts new values at the end of a list |
count() | Returns the number of items with the specified value |
clear() | Deletes all items from the list |
copy() | Returns a copy of the list |
extend() | Adds the items of a list with the current list |
index() | Returns the position at the first occurrence with the specified value |
insert() | Inserts a value at any position in the list |
pop() | Removes a value at any position in the list |
remove() | Removes the specified value from the list |
reverse() | Revereses the order of a list |
sort() | Sorts the items of a list |