Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python extend list

# Basic syntax:
first_list.append(second_list) # Append adds the second_list as an
#	element to the first_list
first_list.extend(second_list) # Extend combines the elements of the 
#	first_list and the second_list

# Note, both append and extend modify the first_list in place

# Example usage for append:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.append(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, [6, 7, 8, 9]]

# Example usage for extend:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.extend(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Comment

extend a list python

#adding two or more elements in a list
list1 = [1,2,3,4,5]
list1.extend([6,7,8])
print(list1)
#output = [1, 2, 3 ,4 ,5, 6, 7, 8]
Comment

list extend() python return type

#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

Python List extend()

Difference between List append() and List extend() method
a =[1,2]
b= [3,4]

# append() method 
a.append(b)
print("Using append() method", a)

x =[1,2]
y= [3,4]


# extend() method 
x.extend(y)
print("Using extend() method", x)
Comment

python list extend

# testing
a = [1, 2]
b = [3, 4]
a.extend(b)
print(a) # [1, 2, 3, 4]
Comment

extend list pyton

animals = ['dog', 'cat']

# tuple
mammals = ('tiger', 'elephant')

animals.extend(mammals)

print('Updated list:', animals)

# dictionary
birds = {'owl': 1, 'parrot': 2}

animals.extend(birds)

print('Updated list:', animals)

#Updated list: ['dog', 'cat', 'tiger', 'elephant']
#Updated list: ['dog', 'cat', 'tiger', 'elephant', 'owl', 'parrot']
Comment

python list extend

# My List
my_list = ['geeks', 'for', 'geeks']
  
# My Tuple
my_tuple = ('DSA', 'Java')
  
# My Set
my_set = {'Flutter', 'Android'}
  
# Append tuple to the list
my_list.extend(my_tuple)
  
print(my_list)
  
# Append set to the list
my_list.extend(my_set)
  
print(my_list)
['geeks', 'for', 'geeks', 'DSA', 'Java']
['geeks', 'for', 'geeks', 'DSA', 'Java', 'Android', 'Flutter']
Comment

Python List extend()

Python Program to extend the list
# Programming list
programming_list = ['C','C#','Python','Java']

frontend_programming =['CSS','HTML','JavaScript']

# add the frontend_progamming list into the existing list
programming_list.extend(frontend_programming)

# Note that iterable element is added to the end of the list
print('The new extended list is :', programming_list)
Comment

PREVIOUS NEXT
Code Example
Python :: split column values 
Python :: how to iterate set in python 
Python :: proper pagination django template 
Python :: find common string in two strings python 
Python :: how to check uppercase in python 
Python :: python new line 
Python :: what is queryset in django 
Python :: python close a socket 
Python :: Python NumPy append Function Syntax 
Python :: ord python3 
Python :: python warnings as error 
Python :: python for loop 
Python :: python - gropuby based on 2 variabels 
Python :: Python NumPy ndarray flat function Example with 2d array 
Python :: how print array in python with clean dublication 
Python :: mathplolib avec date 
Python :: pandas series add word to every item in series 
Python :: python remove header 
Python :: import pycocotools._mask as _mask error Expected 80, got 88 
Python :: Using Python-docx to update cell content of a table 
Python :: First Python Program: Hello World 
Python :: python closing socket good way 
Python :: create time array whith np.datetime64 
Python :: function for permutation sampling 
Python :: python match case example 
Python :: python - remove exta space in column 
Python :: pandas append new column 
Python :: how to chose version of python 
Python :: how to looks like a hacker 
Python :: python oneline if statement 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =