Press ESC to close

Python add to list

0 1273

In Python how can we add elements to a list? We have already said that we can use various methods, in this lesson we will see practical examples.

Exercise – Python add to list

Populate a list of n elements with the first n multiples of 10. Numbers must be inserted at the end of the list. After insertion, display the values ​​of the list and the relative index in the output.
Then change only the elements greater than 30 by subtracting half the number. Display the list again.

Once n has been acquired, the multiples of 10 to be inserted must be automatically generated.

For example if n = 5 then the list must contain these values:

0 * 10, 1 * 10, 2 * 10, 3 * 10, 4 * 10

So I can assume a constant that is worth 10 and a variable that changes value from 0 to 4.

This last variable can be represented directly by the index i of the for loop.

After having populated the list and visualized the elements with the index (attention, the exercise asks to visualize the list after having inserted all the elements, therefore it is necessary to use another for loop that scrolls the list), we modify the elements greater than 30 removing half of the number.

Finally, after the changes, we display the list again.

Here is a possible solution:


n = int(input('How many numbers do you want to enter?: '))
lista_numbers = []
c = 10

for i in range(n):
    lista_numbers.append(i*c)

for i in range(n):
    print('Item in position ', i, ' is ', lista_numbers[i])

#edit elements greater than 30
for i in range(n):
    if lista_numbers[i] > 30:
        lista_numbers[i] //= 2

#visualizzo gli elementi modificati e l'indice
for i in range(n):
    print('Item in position ', i, ' is ', lista_numbers[i])

Python add to list – second exercise

Given two numbers, a and b, build a list with an ascending sequence from the first to the second number inclusive. Numbers must be placed at the top of the list. After entering, view the list with its index.
Then modify each element by multiplying it by 2. Finally view the modified list.

N.B. If b is smaller than a the empty list will be displayed.

The initial difficulty lies in using the insert method to insert numbers at the top of the list.

So to insert an ascending sequence that goes from a to b, using insert, I must first insert the highest number, that is b.

This is why it is convenient to simply invert the range of the for.


for i in range(b,a-1,-1):
    numeri.insert(0,i)

So if for example a = 5 and b = 10 then I have to output the list of numbers = [5,6,7,8,9,10].

To do this I consider the range with i ranging from 10 to 5 in steps of -1.

The first number entered at the top of the list will therefore be 10, then 9, then 8 and so on.

N.B. Using append I can simply write like this:


for i in range(a,b+1):
    numeri.append(i)

But the exercise required the use of inserts.

Then I simply modify the list by multiplying each element by 2.

Here is the complete code:


numbers = []

a = int(input('insert a number'))
b = int(input('insert a number'))

for i in range(b, a-1, -1):
    numbers.insert(0, i)
    
for i in range(len(numbers2)):
    print(i, numbers [i])

#modified list
for i in range(len(numbers)):
    numbers[i] * = 2

for i in range(len(numbers)):
    print(i, numbers[i])

Conclusions

In this section we have dealt with some exercises on how to add elements to list in Python, in the next lesson I propose others.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot

Other stories

Python for loop examples

Next Story

While exercises Python

Previous Story