App Name

RecallCMS

Greeting Prefix

Auto

Learner Name

Aspirant

Home Subtitle

Spaced Repetition (SM-2) Master PYQ Decks

AdSense Publisher ID

ca-pub-0000000000000000

AdSense Auto Ads (ON or OFF)

OFF

Rest Stop Ad Slot ID

1234567890

Home and Game Page Ad Slot ID

0987654321

Default Batch Size

20

Tuesday, April 23, 2019

Python tutorials 2: let's talk on LISTS

Lists
In our last chapter we talked about variables and data types in python.

Today we will deal with lists.

Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner.

This section will give you a more deeper insight on lists

Here is an example of how to build a list.

Two common operations are indexing and assigning to an index position. Both of these operations take the same amount of time no matter how large the list becomes. When an operation like this is independent of the size of the list they are O(1)O(1).

Another very common programming task is to grow a list. There are two ways to create a longer list. You can use the append method or the concatenation operator. The append method is O(1)O(1). However, the concatenation operator is O(k)O(k) where kk is the size of the list that is being concatenated. This is important for you to know because it can help you make your own programs more efficient by choosing the right tool for the job.

Let’s look at four different ways we might generate a list of n numbers starting with 0. First we’ll try a for loop and create the list by concatenation, then we’ll use append rather than concatenation. Next, we’ll try creating the list using list comprehension and finally, and perhaps the most obvious way, using the range function wrapped by a call to the list constructor.

def test1(): l = []
for i in range(1000): l = l + [i]
def test2(): l = []
for i in range(1000): l.append(i)
def test3(): l = [i for i in range(1000)] def test4(): l = list(range(1000)) list=[1,'abc',2]

Copy Note: Copy and Paste Code into Compiler

Basic List Operations

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.

Python ExpressionResults
Description
len([1, 2, 3])
3
Length[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]
Concatenation['Hi!'] * 4
['Hi!', 'Hi!', 'Hi!', 'Hi!']RepeetMition3 in [1, 2, 3]TrueMembershipfor x in [1, 2, 3]: print x,1 2 3Iteration

Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. For example -

list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : " print list[2] list[2] = 2001; print "New value available at index 2 : " print list[2]

Copy Note: Copy and Paste Code into Compiler
 Open Compiler 

Deleting List Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. For example -

list1 = ['physics', 'chemistry', 1997, 2000]; print list1 del list1[2]; print "After deleting value at index 2 : " print list1

Copy
Note: Copy and Paste Code into Compiler
 Open Compiler 

Indexing, Slicing, and Matrixes

Because lists are sequences, indexing and slicing work the same way for lists as they do for strings.

Assuming following input −

L = ['spam', 'Spam', 'SPAM!']

Python ExpressionResultsDescriptionL[2]'SPAM!'Offsets start at zeroL[-2]'Spam'Negative: count from the rightL[1:]['Spam', 'SPAM!']Slicing fetches sections

Built-in List Functions & Methods

Python includes the following list functions −

Sr.No.Function with
Description
1 cmp(list1, list2)

Compares elements of both lists.

2 len(list)

Gives the total length of the list.

3 max(list)

Returns item from the list with max value.

4 min(list)

Returns item from the list with min value.

5 list(seq)

Converts a tuple into list.

No comments:

Home
⚡ 0d
Good Morning
Aspirant

Spaced Repetition (SM-2) Master PYQ Decks

Loading Master PYQ Decks...

Free Active Recall & Spaced Repetition for Competitive Exams

Practice verified Previous Year Questions (PYQs) and speed calculation drills for SSC CGL, CHSL, MTS, UPSSSC PET, RRB NTPC, Group D, CTET, and State PSC exams. Our SuperMemo-2 (SM-2) engine schedules each flashcard right before you forget it and saves progress offline on your device.

30s
MASTER DECK
Question reference graphic
Answer & Explanation
Tap anywhere to reveal answer
Practice Games

Select a practice game page to start playing

Batch Complete!

Great work finishing this batch.

0
Reviewed
0
Memorized
0
Remaining
Stats & Backup

Memory Telemetry

0
Tracked Cards
0
Due Today
2.50
Avg Ease
0
Starred PYQs

Offline Progress Backup

Export or restore your IndexedDB spaced-repetition intervals as a portable JSON file.

Page

Loading Page...