Detailed explanation of pop function and append function in python

不言
Release: 2018-05-04 14:17:46
Original
7019 people have browsed it

This article mainly introduces the detailed explanation of the pop function and append function in python. It has certain reference value. Now I share it with you. Friends in need can refer to it

pop() function

1. Description

pop() function is used to remove an element from the list (default is the last element ), and returns the value of the element.

Syntax

##pop() method syntax:

list.pop(obj=list[-1])
Copy after login

2. Parameter

obj – Optional parameter, the object to remove the list element.

3. Return value

This method returns the element object removed from the list.

4. Example

The following example shows how to use the pop() function:

#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc'];
print "A List : ", aList.pop();
print "B List : ", aList.pop(2);
Copy after login

The output result of the above example is as follows:

A List : abc
B List : zara
Copy after login

##append() function

1. Description

The Python list append() method is used to append (add) the incoming object to an existing list.

2. Syntax

The following is the syntax of the append() method-

list.append(obj)
Python
Copy after login

3. Parameters

obj - This is the object to be added to the list.

4. Return value

This method does not return any value, but updates the existing list.

5. Example

The following example shows the usage of the append() method.

#!/usr/bin/python3
list1 = ['C++', 'Java', 'Python']
list1.append('C#')
print ("updated list : ", list1)
Python
Copy after login

When running the above program, it produces the following result-

updated list : ['C++', 'Java', 'Python', 'C#']
Copy after login

Gif Demonstrate the execution process of Python while statement

Related recommendations:

A brief discussion on the change of value after dictionary append to list in python

The above is the detailed content of Detailed explanation of pop function and append function in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!