开发者

How can I create a list in Python?

http://diveintopython3.ep.io/native-datatypes.html

I've found that link but it seems to rely on the fact that I first have to hard create the list. How can I create a list in Python if it's empty at first. I'm coming from a C# background so this is kind of weird so far.

For example the .append() method is what I'm looking for, but it relies on the fact that the list first exists. An开发者_StackOverflowy suggestions?


You can create an empty list like this

L=list()

or

L=[]


As noted by others, you can create an empty list with the list literal operator [].

Its worth noting that you can initialise the list with values if you always need to start with a few values already in place:

a = 5
L = [42, -12, a]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜