Python Commands you need to Create List

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

Python commands

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

How to create List in Python

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]

Free Python List examples video really help you

Advertisements

Author: Srini

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