YouTube Icon

Code Playground.

How to Add Elements to a List in Python (append, extend and insert)

CFG

How to Add Elements to a List in Python (append, extend and insert)

When working with records in Python, you will regularly need to add new components to the rundown. 

The Python list information type has three strategies for including components: 

  • annex() - adds a solitary component to the rundown. 
  • expand() - adds components of an iterable to the rundown. 
  • embed() - embeds a solitary thing at a given situation of the rundown. 

Every one of the three techniques adjust the rundown set up and bring None back. 

Python List append()

The add() technique adds a solitary component to the furthest limit of the rundown . 

The language structure of the add() technique is as per the following: 

list.append(element) 

Where, component is the component to be added to the rundown. 

Here is a model: 

characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

characters.append('Nairobi')

print('Updated list:', characters)
Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi']

The component boundary can be an object of any information type: 

odd_numbers = [1, 3, 5, 7] 

even_numbers = [2, 4, 6]

odd_numbers.append(even_numbers)

print('Updated list:', odd_numbers)

The rundown even_numbers is added as a solitary component to the odd_numbers list. 

Updated list: [1, 3, 5, 7, [2, 4, 6]]

Python List expand() 

The expand() strategy all components of an iterable to the furthest limit of the rundown. 

The punctuation of the expand() technique is as per the following: 

list.extend(iterable) 

Where, iterable is the iterable to be added to the rundown. 

characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

new_characters = ['Nairobi', 'Denver', 'Rio']

characters.extend(new_characters)

print('Updated list:', characters)
Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi', 'Denver', 'Rio']

The contention can be any sort of iterable: 

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']

Python List insert()

The supplement() technique adds a solitary component to the rundown at the predefined record. 

The grammar of the supplement() strategy is as per the following: 

list.insert(index, element) 

Where, file is the file of the component before which to embed, and the component is the component to be embedded in the rundown. In Python the rundown record begins with 0. 

Here is a model: 

fruits = ['raspberry', 'strawberry', 'blueberry'] 

fruits.insert(1, 'cranberry')

print('Updated list:', fruits)
Updated list: ['raspberry', 'cranberry', 'strawberry', 'blueberry']

The component boundary can be an object of any information type: 

numbers = [10, 15, 20, 25] 

squares = [1, 4, 9]

numbers.insert(2, squares)

print('Updated list:', numbers)

The rundown squares is embedded as a solitary component to the numbers list. 

Updated list: [10, 15, [1, 4, 9], 20, 25]

Conclusion

We have told you the best way to add components to a rundown in Python utilizing the attach(), expand(), and embed() strategies. Another approach to add components to a rundown is to utilize the + administrator to link different records. 

On the off chance that you have any inquiries or input, don't hesitate to leave a remark.




CFG