What is Python List?
The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth.
Python has following six built-in types of sequences:
The most common sequences in Python are list and tuple. This tutorial will go through some basic functions for a list.
All sequences can do following operations:
- checking for membership.
- finding the length of a sequence
- finding largest and smallest elements
The list is a most adaptable datatype available in Python. Following example illustrated how to create a list in Python.
European_Sales_List = [“Peter”, “David”, “Mary”,”Joan”]
American_Sales_List = [“John”,”Tony”,”Michelle”,”Mark”]
This will create two lists each of them with 4 elments they are separated by a comma.
Important thing about a list is that items in a list need not be of the same type.
Accessing Values in Lists
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example −
You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.
Update Single Elements
For Example to change the element 1 in the European Sales List from “David” to “Daniel”
Merge two list and store new result into a new list
All_Sales_List = European_Sales_List + American_Sales_List
Append List Elements
There is two options we can add an element into a list:
- Use the “+” Operator
- User the append function
Using the “+” Operator
Following example added a a new element “Albert” into the list “American_Sales_List using the ” + ” operator
Using the append function
Following example added a a new element “Albert” into the list “American_Sales_List using the append function
“+” Operator vs Append Function
The + operation adds the list elements to the original array. You can see that from my previous example, the new element “Albert” was in a square bracket. This means the element “Albert” was being treated as a list before being added to the “American_Sales_List”
The array.append operation inserts the array (or any object) into the end of the original array, which results in a reference to self in that spot (hence the infinite recursion).
The difference here is that the + operation acts specific when you add an array (it’s overloaded like others, see this chapter on sequences) by concatenating the element. The append-method however does literally what you ask: append the object on the right-hand side that you give it (the array or any other object), instead of taking its elements.
Delete List Elements
To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. For example −
Del <List Name> [<index>]
List_name.remove(Element to remove)
Basic List Operations
Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.
In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.
|All_Sales_List = European_Sales_List + American_Sales_List||[‘Peter’, ‘Daniel’, ‘Mary’, ‘Joan’, ‘John’, ‘Tony’, ‘Michelle’, ‘Mark’]||Concatenation|
|[‘Hi!’] * 4||[‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]||Repetition|
|‘Mary’ in All_Sales_List||True||Membership|
|for x in All_Sales_List : print (x, End =’, ‘)||Peter, Daniel, Mary, Joan, John, Tony, Michelle, Mark,||Iteration|