• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Python List

November 19, 2014 by Dhaval Dave

The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type.

Creating a list is as simple as putting different comma-separated values between squere brackets. For example:

#Array/List Defination : 
shopping_list = [1,2,3,”Blog”,”Gohired”];

Accessing Values in Lists:

list1 = [‘A’, ‘B’, 1, 2];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print “list1[0]: “, list1[0]
print “list2[1:5]: “, list2[1:5]


>> list1[0]:  A
>> list2[1:5]:  [2, 3, 4, 5]

Adding/Deleting/Updating Lists:

shopping_list = [1,2,3];

#Array/List operations

shopping_list.append(4); //Adding 
shopping_list.remove(1); //removing 
shopping_list[0]=’DD’// Updating
del shopping_list[3]
print(shopping_list);

List Operations:

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!’] Repetition
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]: print x, 1 2 3 Iteration

List Functions & Methods:

cmp(list1, list2) Compares elements of both lists.
len(list)  Gives the total length of the list.
max(list) Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(seq) Converts a tuple into list.

list.append(obj) : Appends object obj to list
list.count(obj) : Returns count of how many times obj occurs in list
list.extend(seq) : Appends the contents of seq to list
list.index(obj) : Returns the lowest index in list that obj appears
list.insert(index, obj) : Inserts object obj into list at offset index
list.pop(obj=list[-1]) : Removes and returns last object or obj from list
list.remove(obj) : Removes object obj from list
list.reverse() : Reverses objects of list in place
list.sort([func]) : Sorts objects of list, use compare func if given

Examples

shopping_list = [1,2,3];
print shopping_list;
#Array/List operations
shopping_list.append(4);
print(shopping_list);
shopping_list.remove(1);
print(shopping_list);
shopping_list.insert(3,’DD’);
shopping_list.append(4);
print(shopping_list);
print shopping_list.index(4);

Try others like pop, sort and reverse
Guess Output or check it here http://ideone.com/0iA0tM

Similar Articles

Filed Under: Interview Questions Tagged With: Array, python

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Print all nodes that are at distance k from a leaf node

Maximum size of square sub matrix with all 1’s in a binary matrix

Difference between a LinkedList and a Binary Search Tree BST

How Radix sort works

25 horses 5 tracks Find 3 fastest puzzle

Linked List V/S Binary Search Tree

Introduction To Number Theory ( Part 1 )

Get K Max and Delete K Max in stream of incoming integers

Longest Increasing Subsequence

Given a float number convert it into the string WITHOUT using any inbuilt Function

How strtok() Works

Search element in a matrix with all rows and columns in sorted order

simple sql injection

Spanning Tree

Diagonal Traversal of Binary Tree

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

C++ OOPs Part2

Coin Collection Dynamic Programming

Binary Tree in Java

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Max Sum in circularly situated Values

BFS (Breath First Search)

Calculate price of parking from parking start end time prices

Find Pythagorean Triplets in an array in O(N)

LeetCode: Container With Most Water

Amazon Interview On-Campus For Internship – 1

SAP Hiring Off-Campus General Aptitude

Given a string, find the first character which is non-repetitive

Best Java Book | Top Java Programming Book for Beginners

Copyright © 2025 · Genesis Framework · WordPress · Log in