Which functions can you use to add elements to a list?

sort[] method

The sort[] method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.

Example

Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . [period] followed by the method name, i.e., sort including the parentheses.

prices = [238.11, 237.81, 238.91] prices.sort[] print[prices] [237.81, 238.11, 238.91]

type[] function

For the type[] function, it returns the class type of an object.

Example

Here we will see what type of both fam and fam2 are:

fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89] fam ['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]

Let's see what the type of the object is:

type[fam] list

Now, let's look at fam2.

fam2 = [["liz", 1.73], ["emma", 1.68], ["mom", 1.71], ["dad", 1.89]] fam2 [['liz', 1.73], ['emma', 1.68], ['mom', 1.71], ['dad', 1.89]]

Let's see what the type of the object is:

type[fam2] list

These calls show that both fam and fam2 are in fact lists.

append[] method

The append[] method will add certain content you enter to the end of the elements you select.

Example

In this example, let’s extend the string by adding “April” to the list with the method append[]. Using append[] will increase the length of the list by 1.

months = ['January', 'February', 'March'] months.append['April'] print[months]

When you run the above code, it produces the following result:

['January', 'February', 'March', 'April']

Methods to add elements to List in Python

There are four methods to add elements to a List in Python.

  1. append[]: append the object to the end of the list.
  2. insert[]: inserts the object before the given index.
  3. extend[]: extends the list by appending elements from the iterable.
  4. List Concatenation: We can use + operator to concatenate multiple lists and create a new list.

Introduction

In Python, you’ll be able to use a list function that creates a group that will be manipulated for your analysis. This collection of data is named a list object.

While all methods are functions in Python, not all functions are methods. There’s a key difference between functions and methods in Python. Functions take objects as inputs while Methods in contrast act on objects.

Image Source: Google Images

Python offers the subsequent list functions:

  • sort[]: Sorts the list in ascending order.
  • type[list]: It returns the class type of an object.
  • append[]: Adds one element to a list.
  • extend[]: Adds multiple elements to a list.
  • index[]:Returns the first appearance of a particularvalue.
  • max[list]: It returns an item from the list with a max value.
  • min[list]: It returns an item from the list with a min value.
  • len[list]: It gives the overall length of the list.
  • clear[]: Removes all the elements from the list.
  • insert[]:Adds a component at the required position.
  • count[]:Returns the numberof elements with the required value.
  • pop[]:Removes the element at the required position.
  • remove[]:Removes the primary item with the desired value.
  • reverse[]:Reverses the order of the list.
  • copy[]: Returns a duplicate of the list.

Python - Add List Items

❮ Previous Next ❯

Append Items

To add an item to the end of the list, use the append[] method:

Example

Using the append[] method to append an item:

thislist = ["apple", "banana", "cherry"]
thislist.append["orange"]
print[thislist]

Try it Yourself »

Python lists

last modified December 15, 2021

In this part of the Python programming tutorial, we cover Python lists in more detail.

Python List insert[]

In this tutorial, we will learn about the Python List insert[] method with the help of examples.

The insert[] method inserts an element to the list at the specified index.

Example

# create a list of vowels vowel = ['a', 'e', 'i', 'u']

# 'o' is inserted at index 3 [4th position] vowel.insert[3, 'o']

print['List:', vowel] # Output: List: ['a', 'e', 'i', 'o', 'u']

Python List append[]

In this tutorial, we will learn about the Python list append[] method with the help of examples.

The append[] method adds an item to the end of the list.

Example

currencies = ['Dollar', 'Euro', 'Pound']

# append 'Yen' to the list currencies.append['Yen']

print[currencies]

# Output: ['Dollar', 'Euro', 'Pound', 'Yen']

Video liên quan

Bài mới nhất

Chủ Đề