【问题标题】:How can i create list and add elements in python?如何在 python 中创建列表和添加元素?
【发布时间】:2020-08-26 15:27:00
【问题描述】:

我在下面添加了用于创建“列表”和添加元素的所有方法的可读代码。这是代码:

创建列表。使用 append(),insert() 添加元素

# Create lists
list1 = [1,4,6,8] # integer list.
list2 = [] # Empty List.
list3 = [1.2,'Raj',1] # mixed list.

# Print Created lists
print(list1)
print(list2)
print(list3)

# Add element using append(). Elements will be added at the last of list.
list1.append(11)
list1.append(12)
print(list1)

# Add elements in an empty list using for loop and append()
list2 = []
string = 'abcdef'
for ele in string:
list2.append(ele)
print(list2)

# Add element using insert(). Element will add at any position using index
list4 = ['r','j','s','h']
list4.insert(1,'a')
list4.insert(3,'e')
print(list4)

【问题讨论】:

  • 有什么问题
  • 代码将毫无问题地执行。没有问题。

标签: python-3.x list collections insert append


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
  • 2018-10-16
  • 2014-12-25
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
  • 2021-12-28
相关资源
最近更新 更多