Python 3 Lists

A list is a value that contains multiple values in an ordered sequence.

Example of list : fruits = ['Banana', 'Apple', 'Carrot', 'Strawberry']

A list begins with an opening bracket and ends with a closing bracket.

Values inside the list are called items, and they are separated by commas.

Getting Individual Values in a List with Indexes:

Consider the above list for example, the python code fruits[0] would evaluate to 'Banana' and fruits[1] would evaluate to 'Apple' and so on.

The integer inside the square bracket that follows the list is called an index. The first value in the list is at index 0, the second value is at index 1, the third value is at index 2 and so on.

Indexes can only be integer values not floats. For example, fruits[1.0] will give error.

Lists can also contain other list values. The values in these lists of lists can be accessed using multiple indexes, for example:

fruits = [['Banana', 'Apple', 'Carrot', 'Strawberry'],[10,20,30,40,50]]

First index now dictates which list value to use and the second indicates the value within the list value.

E.g. fruits[0][1] prints "Apple"

If you use only one index, the program will print the full list value at that index.

Negative Indexes:

While indexes starts at 0 and go up, you can also use negative indexes for the index.
The integer value -1 refers to the last index in the list, the value -2 refers to the second-last index in a list and so on.

E.g. fruits = ['Banana', 'Apple', 'Carrot', 'Strawberry']

fruits[-1] = 'Strawberry'
fruits[-2] = 'Carrot'

Getting Sublists with Slices:

Just an index can get a single value from a list, a slice can get several values from a list, in the form of a new list. A slice is typed between square brackets like an index, but it has two integers separated by a colon.

E.g.: fruits[2] = 'Carrot'
fruits[1:3] = ['Apple', 'Carrot']

In a slice, the first integer is the index where the slice starts. The second integer is the index where the slice ends. A slice goes up to, but will not include, the value at the second index.  A slice evaluates to a new list value.

As a shortcut, you can leave out one or both of the indexes on either side of the colon in the slice. Leaving out the first index is the same as using 0, or the beginning of the list. Leaving out the second index is the same as using the length of the list, which will slice to the end of the list

fruits[:] will print the whole list.

Getting a List's length with len():

The len() function will return the number of values that are in a list value passed to it, just like it can count the number of characters in a string value.

E.g. : len(fruits) = 4

Changing Values in a List with Indexes:

You can use an index of a list to change the value at the index.
fruits[1] = 'Watermelon'
fruits = ['Banana', 'Watermelon', 'Carrot', 'Strawberry']

List Concatenation and Replication :

The + operator can be used to combine two lists to create a new list value in the same way it combines two strings into a new string value. The * operator can also be used with a list and an integer value to replicate the list.

Removing Values from Lists and del statements:

The del statement will delete values at an index in a list. All of the values in the list after the deleted value will be moved up one index.

The del statement can also be used on a simple variable to delete it, as if it were an "unassignment" statement.

In practice, you almost never need to delete simple variables. The del statement is mostly used to delete values from lists.

Finding a Value in a List with the Index() Method:

List values have an index() method that can be passed a value, and if that value exists in the list, the index of the value is returned. If the value isn't in the list, then python produces a ValueError  error.

E.g. fruits.index('Watermelon') will return 1

When there are duplicates of the value in the list, the index of the first appearance is returned.

Adding values with append() and insert() method:

append() method adds the argument to the end of the list whereas insert() method can insert value at the specified index.

E.g. : fruits.append('pineapple')
fruits = ['Banana', 'Watermelon', 'Carrot', 'Strawberry', 'pineapple']

fruits.insert(2, 'Orange')
fruits = ['Banana', 'Watermelon', 'Orange', 'Carrot', 'Strawberry', 'pineapple']

Removing Values from Lists with remove():

The remove() method is passed the value to be removed from the list it is called on. The del statement is good to use when you know the index of the value you want to remove from the list. The remove() method is u good when you know the value you want to remove from the list.

E.g.: fruits.remove['Banana']

Sorting the values in a List with the sort() method:

Lists of number values or lists of strings can be sorted with the sort() method.

After calling fruits.sort()

['Banana', 'Carrot', 'Strawberry', 'Watermelon', 'pineapple'] 

You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order.

fruits.sort(reverse=True)
['pineapple', 'Watermelon', 'Strawberry', 'Carrot', 'Banana']

You cannot sort lists that have both numbers values and string values in them.

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto