Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic list question
#1
>>> mylist = ['apple', 'banana', 'cherry']
>>> mylist[1:2] = ['kiwi', 'mango'] ## this translate to mylist[1] = ['kiwi', 'mango'] , so does this means the list is apended to mylist and it should be somethign like this [['apple',[ 'kiwi', 'mango'],  'cherry'] but why it is the way it is i.e ['apple', 'kiwi', 'mango', 'cherry']
>>> print (mylist)
['apple', 'kiwi', 'mango', 'cherry']
>>> print (mylist[1:2])
['kiwi']
>>>
this translate to mylist[1] = ['kiwi', 'mango'] , so does this means the list is apended to mylist and it should be somethign like this [['apple',[ 'kiwi', 'mango'], 'cherry'] but why it is the way it is i.e ['apple', 'kiwi', 'mango', 'cherry']
Reply
#2
mylist[1:2] = ['kiwi', 'mango'] replaces a slice of mylist with the values in ['kiwi', 'mango']
mylist[1] = ['kiwi', 'mango'] replaces an element in mylist with the list ['kiwi', 'mango']

Slices are interesting.

https://www.30secondsofcode.org/python/s...ssignment/
Reply
#3
Hi,

as already explained @deanhystad, you perform a replacement using a slice operation. If you want to insert something, use the insert method:

>>> some_list = ['apple', 'banana', 'cherry']
>>> another_list = ['kiwi', 'mango']
>>> some_list.insert(1, another_list)
>>> some_list
['apple', ['kiwi', 'mango'], 'banana', 'cherry']
>>>
Please note that a) the list is modified in place and b) it is not exactly doing what you are looking for, as banana is still in the list (could be removed by the pop method of lists.

What you intend to do is possible, but I think only by a bit more slicing and rejoing the list:

>>> some_list = ['apple', 'banana', 'cherry']
>>> another_list = ['kiwi', 'mango']
>>> result = some_list[0:1]
>>> result.append(another_list)
>>> result
['apple', ['kiwi', 'mango']]
>>> result.append(some_list[-1])
>>> result
['apple', ['kiwi', 'mango'], 'cherry']
>>>
Maybe there's a more elegant to do that?

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 1,762 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  [solved] Basic question on list matchiing paul18fr 7 3,084 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  Very basic calculator question BoudewijnFunke 4 2,883 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  basic question isinstance tames 5 3,969 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 4,045 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  List index out of range error when attempting to make a basic shift code djwilson0495 4 4,218 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,683 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  Basic coding question with Python Than999 3 3,795 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  Basic List Issue rogueakula 1 2,631 May-18-2019, 06:01 PM
Last Post: snippsat
  basic question???????? cemdede 2 2,999 Jan-18-2019, 03:05 AM
Last Post: ntd9395

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020