How to Create List in Python Top Commands

In Python, the manipulating list also called arrays, is the main task while you are dealing or working with data. Stanford University in its Python tutorial added the below best examples.


Python List Commands

I am giving here for your reference. The hash given below is just a comment. It is not command.

Useful Commands to Create a List

Create a new list

empty = []
letters = [‘a’, ‘b’, ‘c’, ‘d’]
numbers = [2, 3, 5]

Lists can contain elements of different types

mixed = [4, 5, “seconds”]

Append elements to the end of a list

numbers.append(7)
numbers == [2, 3, 5, 7]
numbers == [2, 3, 5, 7]
numbers.append(11)
numbers == [2, 3, 5, 7, 11]

Access elements at a particular index

numbers[0] # => 2
numbers[-1] # => 11

Slice Lists rules to apply

letters[:3] # => ['a', 'b', 'c']
numbers[1:-1] # => [3, 5, 7]

Other Lists

x = [letters, numbers]
x # => [['a', 'b', 'c', 'd'], [2, 3, 5, 7, 11]]
x[0] # => ['a', 'b', 'c', 'd']
x[0][1] # => 'b'
x[1][2:] # => [5, 7, 11]

How to Create List in Python a Video

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.