Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

append vs. extend in list python

#List [], mutalbe

# Difference between List append() and List extend() method
# append() adds an single object to the list
# extend() unpacks the passed object and adds all elements in that object individually to the list

# append() method 
a = [1,2]
b = [3,4]
a.append(b)		#append() adds one element to the list
print("Using append() method", a)	#[1, 2, [3, 4]]

# extend() method
x =[1,2]
y= [3,4]
x.extend(y)		#extend() adds multiple elements
print("Using extend() method", x)	#[1, 2, 3, 4]

sample_list = []
sample_list.extend('abc')       #extend() unpacks the string and pass each char individually
print(sample_list)              #['a', 'b', 'c']

# plus assignment, augmented assignment, concatenate merge the 2 lists, works like extend()
c =[1,2]
d = [3,4]
print(c + d)      #[1, 2, 3, 4]   #concatenate works like extend()
c += d
print("Using augmented assignment method", c)	#[1, 2, 3, 4]
Comment

List Method: list append vs extend

# list append vs extend

e = [1,2,3]
e.append(["a",[8]])
print(e)
# [1, 2, 3, ['a', [8]]]

u = [1,2,3]
u.extend(["a",[8]])
print(u)
# [1, 2, 3, 'a', [8]]
Comment

PREVIOUS NEXT
Code Example
Python :: what is a good django orm cookbook 
Python :: python lvl up 
Python :: Use Python to calculate (((1+2)*3)/4)^5 
Python :: print function in python 
Python :: How to make a script that reads from Database and then writes to the csv file and then uploads the file to Google Drive in python 
Python :: how to get the access of python on cmd 
Python :: python sns save plot lable axes 
Python :: triu function in numpy 
Python :: how to get device hwid cmd 
Python :: how to count the appearance of number or string in a list python 
Python :: select series of columns 
Python :: list the contents of a package python 
Python :: Python - Cara Mengurutkan String Secara alfabet 
Python :: flash not defined python flask 
Python :: how to add 2 integers in python 
Python :: clicking items in selenium 
Python :: make a function that accepts any nuber of arguments python 
Python :: what does it mean when i get a permission error in python 
Python :: how to export schema in graphene django 
Python :: random module randint 
Python :: date component 
Python :: django array of dates 
Python :: how to remove na values in r data frame 
Python :: createdb psql 
Python :: python identify array 
Python :: tcs question 
Python :: Python turtle (built in shape) image size 
Python :: python pipe where 
Python :: pytest using tempfile 
Python :: check entries smaller 0 after groupby 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =