-1

I want to create a list which should be stored under mylist.

#Here is my code.
mylist = [10,20,30,'apple',True,8.10]
mylist

output: [10, 20, 30, 'apple', True, 8.1]

#I want to add two numbers to the list using append()
mylist.append(30)
mylist.append(40)

But the out put has shown as below by getting added the elements two times.

mylist

output: [10, 20, 30, 'apple', True, 8.1,30,40,30,40]

Then I wanted to remove the extra numbers from the list 30,40

mylist.remove(40)
mylist.remove(30)

However I was able to remove both the elements contain 40 and one 30

mylist
output: [10, 20, 'apple', True, 8.1, 30]

#Now I want to move the last element to the 3rd position which should look like the below output.

mylist
desired output: [10,20,30,'apple',True, 8.1]

And finally I want to add my two extra elements 30,40 to 'mylist' using append() with out any repetitions.

Mahesh
  • 11
  • 9
  • Does this answer your question? [How can I reorder a list?](https://stackoverflow.com/questions/2177590/how-can-i-reorder-a-list) – mr_mooo_cow Aug 15 '22 at 23:38
  • Somehow you have a problem with your work environment. The append() function DOES NOT add an element twice - that would not make any sense. No one could use a language that worked that way. I can't guess why you're seeing this but it's not the way Python works. – Paul Cornelius Aug 15 '22 at 23:44
  • also as for adding an element to a specific index, try the `list.insert()` method – Christian Trujillo Aug 15 '22 at 23:46
  • Cannot reproduce. `30` and `40` are appended to list only once. Save your code the `.py` file ad ru it. – Yuri Ginsburg Aug 15 '22 at 23:46
  • Always strive to make example code that can be copied and reproduce the problem **without changes**. As stated above, `.append` doesn't work that way. Read the [mcve] guidelines. – Mark Tolonen Aug 15 '22 at 23:51

1 Answers1

0

To move the last element to the 3rd position:

mylist.insert(2, mylist[-1])
mylist.pop(-1)

To add the elements:

mylist.append(30)
mylist.append(40)

Make sure you only call the append function once. Also check the list before calling append doesn't alredy have the 30 and 40.

itogaston
  • 389
  • 2
  • 6