Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Linux

How to Add an Item to List Using Append() Command

by Editorial Staff
July 6, 2019
in Linux
Reading Time: 3 mins read

We have a list of numbers or strings, and we want to append items to a list. Basically, we can use the append method to achieve what we want.

The append() method adds a single item to the existing list. It doesn’t return a new list; rather it modifies the original list.

The syntax of append() method

list.append(item)

append() Parameters

The append() method takes a single item and adds it to the end of the list. The item can be numbers, strings, another list, dictionary etc.

Table of Contents
1) Adding Element to a List
2) Adding list to a list
3) Adding element of list to a list
4) Adding element to a list with for loop
5) Adding element to a list with while loop
6) Appending two array using numpy module
Conclusion

1)  Adding Element to a List

Let’s look adding element to a list with an example

  # fruit list
  fruit = ['orange', 'apple', 'banana']

  # an element is added
  fruit.append('pineapple')

  #Updated Fruit List
  print('Updated fruit list: ', fruit)
  output
  Updated animal list: ['orange', 'apple', 'banana', 'pineapple']

as you see , 'pineapple' element has been added as last element.

2) Adding list to a list

Let’s see how to add list to a list

  # fruit list
  fruit = ['orange', 'apple', 'banana']

  # another list of green fruit
  green_fruit = ['green apple', 'watermelon']

  # adding green_fruit list to fruit list
  animal.append(green_fruit)

  #Updated List
  print('Updated animal list: ', fruit)
  output
  Updated animal list: ['orange', 'apple', 'banana', ['green apple', 'watermelon']]

As you see when append green_fruit list to fruit list, it goes as a list not two element.

3) Adding element of list to a list

Here we wil use extend() method to add element of list to another list, we will use the same pravious example to see the difference.

  # fruit list
  fruit = ['orange', 'apple', 'banana']

  # another list of green fruit
  green_fruit = ['green apple', 'watermelon']

  # adding green_fruit list to fruit list
  animal.extend(green_fruit)

  #Updated List
  print('Updated animal list: ', fruit)
  output
  Updated animal list: ['orange', 'apple', 'banana', 'green apple', 'watermelon']

As we saw, the green_fruit list has been added as elements not as a list to fruit list.

4) Adding element to a list with for loop

We will use For loop to append groups of element to list.

  # numbers list
  numbers = []

  # use for loop to fill numbers list with elements
  for i in range(10):
    numbers.append(i)

  #Updated List
  print('Updated numbers list: ', numbers)
  output
  Updated numbers list:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We made empty list numbers and used for loop to append numbers in range from 0 to 9, so for loop in frist working append 0 and check number 2 in range or not, if in range append it and so on until reaching number 9, which add it and for loop stop working.

5) Adding element to a list with while loop

We will use While loop to append groups of element to list.

  # temperature list
  temperature = []

  # use while loop to fill temperature list with temperature degrees
  degree_value = 20
  degree_max = 50
  while degree_value <= degree_max:
      temperature.append(degree_value)
      degree_value += 5

  #Updated Temperature List
  print('Updated temperature list: ', temperature)
  output
  Updated temperature list:  [20, 25, 30, 35, 40, 45, 50]

we made empty list temperature and put start point degree_value and limit point degree_max and said, while degree_value less than or equal degree_max, append this value to temperature list, after that increase the value of degree_value five degrees, while loop will work until degree_value equal degree_max and stop working.

6) Appending two array using numpy module

We will use append method in numpy module to append two arrays.

  # import numpy module
  import numpy as np

  A = np.array([3])
  B = np.array([1,5,5])
  C = np.append(A, B)

  #print array C
  print('Array C: ', C)
  output
  Array C:  [3 1 5 5]

Note: you should install numpy module first by this command $ pip3 install numpy.

Conclusion

In this article, we have learned how to add an item in a list using Python append() command. Please let me know if you have any questions.

Related Posts

Apache, MySQL Performance

How to Optimize Apache, MySQL Performance for 1GB RAM VPS Centos/RHEL

April 7, 2024
Top 5 MySQL Performance Tuning Tips

Top 5 MySQL Performance Tuning Tips

January 25, 2023

Top Commands to Check the Running Processes in Linux

May 27, 2022

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022

How Common Signals are Used in Kubernetes Pod Management

November 20, 2021

How to Transfer Windows 10 to SSD?

April 14, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Articles

  • 15 Best Free OCR Software for Windows 10/11 in 2025
  • How a Truck Accident Lawyer Protects Your Rights After a Crash
  • 15 Best VoIP Phone Services in 2025
  • 25 Incident Management Reporting Tools in 2025
  • How Smart Incident Reporting Tools Are Reshaping Modern Workplace Safety
  • 16 Free Password Manager for Mac in 2025
  • 13 HIPAA Compliant Credit Card Processing in 2025

Related Posts

None found

  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.