Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list
#1
Hi friends can anyone explain this for me
def test1():
    l = []
    for i in range(1000):
        l = l + [i]
how this statement
[python]
for i in range(1000):
l = l + [i]

appends the value of i in the list l ???
Reply
#2
addition of lists is same as list.extend()

>>> l1 = [1,2]
>>> l2 = [3, 4]
>>> l1+l2
[1, 2, 3, 4]
>>> l1.extend(l2)
>>> l1
[1, 2, 3, 4]
>>>

of course in python2 you can do
l = range(1000)
or in python3

l = list(range(1000))
Reply
#3
got your point. thanks man
Reply


Forum Jump:

User Panel Messages

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