Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error #514

Open
Meshackdd opened this issue Apr 24, 2024 · 3 comments
Open

Syntax error #514

Meshackdd opened this issue Apr 24, 2024 · 3 comments

Comments

@Meshackdd
Copy link

inserting items into a List

syntax

lst = ['item1', 'item2']
lst.insert(index, item)

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.insert(2, 'apple') # insert apple between orange and mango
print(fruits) # ['banana', 'orange', 'apple', 'mango', 'lemon']
fruits.insert(3, 'lime') # ['banana', 'orange', 'apple', 'lime', 'mango', 'lemon']
print(fruits)

The above code isn't running for me, keeps showing me ERROR message, same with adding g removing lists, removing items using pop.
Is it that theirs an extension I need to add or download on my VS code??? Please HELP...

@KarinaNi
Copy link

Could you please describe the error? Put the error message?

@n00b0dyy
Copy link

There was an error in the code because the variable index and item were not defined before using them in the insert() method on the list lst.

lst = ['item1', 'item2']
index = 1 ###here you can put example index variable###
item = 'new_item' ###here is your new_item variable###
lst.insert(index, item) ###now method insert() can operate on defined variables.###

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.insert(2, 'apple') # insert apple between orange and mango
print(fruits) # ['banana', 'orange', 'apple', 'mango', 'lemon']
fruits.insert(3, 'lime') # ['banana', 'orange', 'apple', 'lime', 'mango', 'lemon']
print(fruits)

###Note that if you defined these values after calling the insert() method, the code would also not work, since the values must be defined before using the method.###

@n00b0dyy
Copy link

You can also try print(lst) to make sure that your code works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants