home PYTHONJAVA
 

Python3 list

Sequences are the most basic data structure in Python.Each element in the sequence is assigned a number - its position, or index,the first index is 0 , the second index is 1, and so on.

Python has built-in types of 6 sequences, but the most common are lists and tuples.

The sequence can be done by indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.

The list is the most commonly used Python data type, and it can appear as a comma-separated value in square brackets.

List of data items does not need to have the same type

Create a list by enclosing the different data items separated by commas in square brackets. As shown below:

list1 = ['Google' , 'Welookups', 1997 , 2000]; list2 = [1, 2, 3, 4 , 5 ]; list3= ["a", "b" , "c", "d"];

Like the index of a string, the list index starts at 0. The list can be intercepted, combined, and so on.


Access the value in the list

Use the subscript index to access the values in the list. You can also use the square brackets to intercept characters as follows:

Instance (Python 3.0+)

#!/usr/bin/python3 list1 = ['Google', 'Welookups' , 1997, 2000]; list2 = [1, 2, 3, 4 , 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5] )

The output of the above example:

list1[0]: Google
List2[1:5]: [2, 3, 4, 5]

Update list

You can modify or update the data items in the list. You can also use the append() method to add list items as follows:

Instance (Python 3.0+)

#!/usr/bin/python3 list= ['Google', 'Welookups' , 1997, 2000] print ("The third element is: ", list[2]) list[2] = 2001 print ("The third element after the update is: ", list[2])

Note: We will discuss the use of the append() method in the next section

The output of the above example:

The third element is   : 1997
The third element after the update is  : 2001

Delete list elements

You can use the del statement to remove elements of a list, as in the following example:

Instance(Python 3.0+)

#!/usr/bin/python3 list = ['Google', 'Welookups', 1997, 2000] print ("Original list : ", list) del list[2] print ("Delete the third element : ", list)

The above example output:

Original list : ['Google', 'Welookups', 1997, 2000]
Delete the third element : ['Google', 'Welookups', 2000]

note:We will discuss the use of the remove() method in the next section.


Python list script operator

List operators for + and * are similar to strings. The + sign is used for the combined list and the * is used for the repeat list.

As shown below:

Python ExpressionResults Description
len([1, 2, 3])3length
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6] combination
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repeat
3 in [1, 2, 3]TrueIs the element present in the list
for x in [1, 2, 3]: print(x, end=" ")1 2 3iteration

Python list interception and stitching

Python's list interception and string manipulation types are as follows:

L=['Google', 'Welookups', 'Javacodegeeks']

Operation:

Python ExpressionResults Description
L[2]'Javacodegeeks'Read the third element
L[-2]'Welookups'Read the second last element from the right: count from the right
L[1:]['Welookups', 'Javacodegeeks']Output all elements from the second element
>>>L=['Google', 'Welookups', 'Javacodegeeks'] >>> L[2] 'Javacodegeeks' >>> L[-2] 'Welookups' >>> L[1:] ['Welookups', 'Javacodegeeks'] >>>

The list also supports stitching:

>>>squares = [1, 4, 9, 16, 25] >>> squares += [36, 49, 64, 81, 100] >>> squares [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>>

nested list

Use nested lists to create additional lists in the list, for example:

>>>a = ['a', 'b', 'c'] >>> n = [1, 2, 3] >>> x = [a, n] >>> x [['a', 'b', 'c'], [1, 2, 3]] >>> x[0] ['a', 'b', 'c'] >>> x[0][1] 'b'

Python list function & method

Python contains the following functions:

serial numberfunction
1len(list)
Number of list elements
2max(list)
Return the maximum number of list elements
3min(list)
Return the minimum value of the list element
4list(seq)
Convert tuples to lists

Python includes the following methods:

serial numbermethod
1list.append(obj)
Add a new object to the end of the list
2list.count(obj)
Statistics an element in the list Number of occurrences
3list.extend(seq)
Add another one at the end of the list Multiple values ​​in a sequence (extending the original list with a new list)
4list.index(obj)
Find one from the list The index position of the first match
5list.insert(index, obj)
Insert object into list< /td>
6list.pop([index=-1])
Remove An element in the list (the default last element) and returns the value of the element
7list.remove(obj)
Remove a value from the list First match
8list.reverse()
Reverse list elements
9 list.sort( key=None, reverse=False)
Yes Sort the original list
10list.clear()
Clear the list
11list.copy()
Copy List





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.