-1

I run this code on my desktop and and I enter watermelon and code run ok but it split watermelon letter by letter

fruits = ['apple','pine','grape','mango','orange']
fruits[1:3] = input('Enter a fruit:')
print(fruits)

Output:

Enter a fruit: watermelon 
['apple','w','a','t','e','r','m','e','l','o','n','mango',orange']

Expected output:

['apple','watermelon','mango','orange']
Barmar
  • 741,623
  • 53
  • 500
  • 612
Python learner
  • 1,159
  • 1
  • 8
  • 20

4 Answers4

3

As the comments state, when executing fruits[1:3], you're creating a slice assignment which slices the word which was input and stores it in the 1 and 2 indexes (3 is excluded).

If you were just trying to add another fruit to the array of fruits,

fruits = ['apple','pine','grape','mango','orange']
fruit = input('Enter a fruit: ')
fruits.append(fruit)
print(fruits)

Alternatively, if you really wanted to replace the 2nd and 3rd places, you can do this,

fruits = ['apple','pine','grape','mango','orange']
fruits[1:3] = [input('Enter a fruit: ')]
print(fruits)

Which will produce, ['apple', 'watermelon', 'mango', 'orange']

Kypps
  • 326
  • 2
  • 5
1

Following on what Barmar commented, try this code.

fruits = ['apple','pine','grape','mango','orange']
fruits[1:3] = [input('Enter a fruit:')]
print(fruits)

It will effectively replace second and third element in array (what I assume you wanted to achieve)

1

Since you're doing slice assignment, the source will be treated as a sequence. The slice [1:3] will be replaced by each element of the source sequence separately.

When a string is used as a sequence, each character is a separate element, so it gets split up and inserted into the list.

If you want to replace the slice with the whole string, wrap it in a list.

fruits[1:3] = [input('Enter a fruit:')]
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Perfect! Thanks for taking your time:) – Python learner Jan 06 '22 at 17:39
  • Sorry for bothering you. But, can you tell me why fruit[1:1] adds "watermelon" without removing other elements? – Python learner Jan 14 '22 at 05:47
  • 1
    Because a slice doesn't include the end index. So the slice `[1:1]` is a zero-length slice, and nothing is removed. – Barmar Jan 14 '22 at 15:48
  • Thanks again. But, I forgot to ask the actual thing . fruits[0:0] is added between fruits[0] and fruits[1]. Likewise, fruits[1:1] is fruits[1] and fruits[2]. I would like to know how this is happening? – Python learner Jan 15 '22 at 06:20
  • 1
    When you assign to a slice, it deletes the original slice and then inserts the source list in its place. If the original slice is empty, it just inserts. – Barmar Jan 15 '22 at 06:21
  • 1
    The insertion moves the following elements up to make room. – Barmar Jan 15 '22 at 06:22
  • 1
    See https://stackoverflow.com/questions/10623302/how-does-assignment-work-with-list-slices – Barmar Jan 15 '22 at 06:22
-1

Convert your input to a list explicitly and then assign to your original list.

newton
  • 1
  • 3