Python - Copying a list

Table of contents

1. Using = operators

2. Using the copy() method

3. Using the slicing technique

4. Using the extend() method

5. Using the append() method

6. Using list comprehension


"""
Original List ---> l_0 : list_0

After Cloning :
l_1 : list_1
l_2 : list_2
l_3 : list_3
l_4 : list_4
l_5 : list_5
l_6 : list_6
"""
l_0 = [1,2,3,4,5,6,7,8,9,10]
print(f'list_0: {l_0}')

1. Using = operators

l_1 = l_0
print(f'list_1: {l_1}')

2. Using the copy() method

l_2 = l_0.copy()
print(f'list_2: {l_2}')

3. Using the slicing technique

l_3 = l_0[:]
print(f'list_3: {l_3}')

4. Using the extend() method

l_4 = []
l_4.extend(l_0)
print(f'list_4: {l_4}')

5. Using the append() method

l_5a = []
for i in range(len(l_0)): l_5a.append(l_0[i])
print(f'list_5a: {l_5a}')

l_5b = []
for j in l_0: l_5b.append(j)
print(f'list_5b: {l_5b}')

6. Using list comprehension

l_6 = [k for k in l_0]
print(f'list_6: {l_6}')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chase_Yi

学无止境~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值