Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

append extend 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

PREVIOUS NEXT
Code Example
Python :: python split by first match 
Python ::  
Python ::  
Python :: python elapsed time module 
Python :: install virtual environments_brew 
Python ::  
::  
Python :: make a condition statement on column pandas 
Python :: python selenium send keys enter send 
Python :: python program to print the fibonacci sequence 
Python :: python split list 
Python ::  
Python :: convert a string into a list in Python 
Python :: matplotlive y axis 
Python :: binary gap python 
Python :: download pytz python 
Python :: python delete all occurrences from list 
Python :: delete row if contains certain text pandas 
Python :: ordered dictionary 
Python :: tiff to jpg in python 
Python :: exclude serializer 
Python :: python repet x time 
Python :: How to calculate distance without numpy 
Python :: cv2 read rgb image 
Python :: dict to attr python 
Python :: merge a list of dictionaries python 
Python :: BURGERS2 
Python :: find commonalities in dictionary python 
Python :: python schedule task every hour 
Python :: get url param in get django rest 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =