添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Tutorial

How To add Elements to a List in Python

Updated on June 17, 2024
author

Pankaj

How To add Elements to a List in Python

Introduction

DigitalOcean App Platform . Let DigitalOcean focus on scaling your app.

Prerequisites

  • How to Code in Python 3 series or using VS Code for Python .
  • This tutorial was tested with Python 3.9.6.

    append()

    insert()

    extend()

    List Concatenation

    concatenate multiple lists, you can use the + operator. This will create a new list, and the original lists will remain unchanged.

    evens = [2, 4, 6]
    odds = [1, 3, 5]
    nums = odds + evens
    print(nums)  # [1, 3, 5, 2, 4, 6]
    

    This example added the list of evens to the end of the list of odds. The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

    Conclusion

  • Python List
  • Python.org Docs
  • Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about our products

    About the authors
    Default avatar
    Pankaj

    author

    Still looking for an answer?

    Ask a question Search for more help

    Was this helpful?
    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 6, 2020

    list1 = [“M”, “na”, “i”, “Ke”] list2 = [“y”, “me”, “s”, “lly”] list3 = [i + j for i,j in zip(list1, list2)] How is the above different to this; list3 = list() for i,j in zip(list1, list2): list3 = i + j how are these 2 codes different from one another?

    - Asad Jaffer

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 23, 2020

    how to use add method like i mean list.add(x)… explain