# Question regarding lists

Page 1 of 1

## 4 Replies - 498 Views - Last Post: 10 January 2013 - 11:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=306319&amp;s=bcfbd6bf42398c94453fa19c671adbc9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 darek9576

• D.I.C Lover

Reputation: 199
• Posts: 1,696
• Joined: 13-March 10

# Question regarding lists

Posted 09 January 2013 - 08:16 AM

Can anyone tell me what is happening behind the scenes when these 2 are executed?:

```listOne = [0 for i in range(100)]

listTwo = [0] * 100

```

I believe (correct me if i am wrong) in the first example it just loops 100 times and calls append(0) on the list.
No idea, how second one is executed.
Is This A Good Question/Topic? 0

## Replies To: Question regarding lists

### #2 andrewsw

• I'm not here to twist your niblets

Reputation: 4204
• Posts: 15,287
• Joined: 12-December 12

## Re: Question regarding lists

Posted 09 January 2013 - 08:36 AM

They both have the same result: creating a list of 0 to 99 elements each with the value 0.

```listOne = [0 for i in range(100)]
print(listOne)
print(len(listOne))
listTwo = [0] * 100
print(listTwo)
print(len(listTwo))
```

The second version just repeats the value. The first version is a list comprehension, which can do a lot more!

```listThree = [i * 2 for i in range(100)]
print(listThree)
```

This post has been edited by andrewsw: 09 January 2013 - 08:37 AM

Was This Post Helpful? 0

### #3 darek9576

• D.I.C Lover

Reputation: 199
• Posts: 1,696
• Joined: 13-March 10

## Re: Question regarding lists

Posted 09 January 2013 - 09:07 AM

I know what they do. This does not answer my question.
Was This Post Helpful? 0

### #4 andrewsw

• I'm not here to twist your niblets

Reputation: 4204
• Posts: 15,287
• Joined: 12-December 12

## Re: Question regarding lists

Posted 09 January 2013 - 09:19 AM

darek9576, on 09 January 2013 - 09:07 AM, said:

I know what they do. This does not answer my question.

listOne = [0 for i in range(100)]

range(100) creates a range of values 0,..,99
for i in refers to each of these values as i (while looping through them)
0 is an expression that refers to each value (i) but, for this example, doesn't actually use the value of i
the square brackets create a list.

This post has been edited by andrewsw: 09 January 2013 - 09:20 AM

Was This Post Helpful? 0

### #5 IceHot

• D.I.C Head

Reputation: 0
• Posts: 220
• Joined: 28-August 12

## Re: Question regarding lists

Posted 10 January 2013 - 11:49 PM

I think he means that he understands that Python is an [binterpreted[/b] language, namely a language that is written in C++. That and he wants to know the C++ code that this would be converted to (I could be wrong on both of these...)
Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }