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 :: multiple line comments 
Python :: streamlit add chart 
Python :: phython to c converter 
Python :: current date to midnight 
Python :: .replace python 
Python :: FileSystemStorage django 
Python :: insert into string python 
Python :: typeerror: 
Python :: python class example 
Python :: loop in python 
Python :: seaborn stripplot range 
Python :: matplot image axis 
Python :: librosa python 
Python :: python hash timestamp 
Python :: python raise filenotfounderror 
Python :: how to write user input to a file in python 
Python :: closures in python 
Python :: xargs in python 
Python :: pandas using eval converter excluding nans 
Python :: division of 2 numbers in python 
Python :: pd df set index 
Python :: How to Access Items in a Set in Python 
Python :: python django login register 
Python :: check if string is python code 
Python :: hexdigest python 
Python :: i++ in python 
Python :: add item to list python 
Python :: euclidean distance 
Python :: gui button in tkinter color 
Python :: Converting time python 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =