YouTip LogoYouTip

Python3 Att List Extend

# Python3.x Python3 List extend() Method [![Image 3: Python3 Lists](#) Python3 Lists](#) * * * ## Description The extend() function is used to append multiple values from another sequence to the end of a list (extending the original list with a new list). ## Syntax The syntax for the extend() method is: list.extend(seq) ## Parameters * seq -- A list of elements. This can be a list, tuple, set, or dictionary. If it is a dictionary, only the keys will be added sequentially to the end of the original list. ## Return Value This method does not return a value, but adds new list content to an existing list. ## Example The following example demonstrates the usage of the extend() function: ## Example #!/usr/bin/python3 list1 = ['Google', '', 'Taobao']list2=list(range(5))# Create a list from 0-4 list1.extend(list2)# Extend the list print("Extended list: ", list1) The output of the above example is: Extended list: ['Google', '', 'Taobao', 0, 1, 2, 3, 4] Different data types: ## Example #!/usr/bin/python3# Language list language = ['French', 'English', 'German']# Tuple language_tuple = ('Spanish', 'Portuguese')# Set language_set = {'Chinese', 'Japanese'} # Add tuple elements to the end of the list language.extend(language_tuple)print('New list: ', language)# Add set elements to the end of the list language.extend(language_set)print('New list: ', language) New list: ['French', 'English', 'German', 'Spanish', 'Portuguese']New list: ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Chinese', 'Japanese'] [![Image 4: Python3 Lists](#) Python3 Lists](#)
← Java Arraylist SizeJava Arraylist Lastindexof β†’