Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Container Types in Python
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Sequences The range function returns a sequence
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 21: Functional Programming in Python COMP 144 Programming Language Concepts Spring.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
Python Data Structures
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
October 17, 2005ICP: Chapter 5: Lists and Dictionaries 1 Introduction to Computer Programming Chapter 5: Lists and Dictionaries Michael Scherger Department.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Lists CS303E: Elements of Computers and Programming.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Lists Michael Ernst CSE 140 University of Washington.
Introduction to Python Aug 22, 2013 Hee-gook Jun.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Map, List, Stack, Queue, Set Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
String and Lists Dr. José M. Reyes Álamo.
Introduction to Higher Order (Functional Programming) (Python) part 2
Python Variable Types.
Lecture 3 Python Basics Part 2.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Embedded Software Development with Python and the Raspberry Pi
Containers and Lists CIS 40 – Introduction to Programming in Python
loops for loops iterate over a given sequence.
Data Structures: Lists
Lecture 10 Data Collections
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
CSc 110, Spring 2018 Lecture 33: Dictionaries
CHAPTER THREE Sequences.
Guide to Programming with Python
Python Data Structures
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Topics Sequences Introduction to Lists List Slicing
CSCE 590 Web Scraping: Lecture 2
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
15-110: Principles of Computing
Topics Sequences Introduction to Lists List Slicing
CSCE 590 Web Scraping: Lecture 2
Introduction to Dictionaries
Sample lecture slides.
COMPUTER SCIENCE PRESENTATION.
Dictionary.
Introduction to PYTHON
Tuple.
Presentation transcript:

Data Structures Akshay Singh

 Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

 List.append(x) – adds “x” to the end of the list  List.extend(L) – adds the list “L” to the end of the list  List.insert(I,x) – adds the “I” item to position “x” in the list.  List.remove(x) – removes the first occurrence of “x” from the list.

 List.pop(I) – remove the element at the “I” position and return it. Removes last element if left blank.  List.index(x) – Returns the position of the first occurrence of “x”  List.count(x) – Returns the number of times “x” appears in the list.

 List.sort() – sorts the list.  List.reverse() – reverses the arrangement of elements in the list.

 Last element in is the first element out.  Use append and pop functions.

 First element in the list is the first the leave.  Use list.append(x) to add to the list and list.popleft() to remove the first element.

 Filter() – returns items from a sequence where the specified function is satisfied.  def f(x): return x % 2 != 0 and x % 3 != 0  filter(f, range(2, 25)) Output: [5, 7, 11, 13, 17, 19, 23]

 Map() – uses the sequence as arguments for the function and returns the results.  def cube(x): return x*x*x  map(cube, range(1, 11)) Output: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

 Reduce() – Sends the first two elements of the sequence to the function, then the result and the third element and so on.  def add(x,y): return x+y  reduce(add, range(1, 11)) Output: 55

 Easy way to work with lists.  Similar to loops.  vec = [2, 4, 6]  [3*x for x in vec] Output: [6, 12, 18]

 The del() statement can be used to remove an element from a list.  It does not return a value.  del a[0] : deletes first element in list “a”  del a[2:4] : deletes third and fourth element  del a[:] : deletes all elements in list “a”

 Tuples are a standard sequence data type.  Tuples are a number of values seperated by commas.  Tuples may be nested.  t = 12345, 54321, 'hello!'  u = t, (1, 2, 3, 4, 5)  ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))

 Collection of elements with no duplicates  Can test for membership of elements  basket = [‘a', ‘o', 'a’, ‘p', ‘o', ‘b']  fruit = set(basket)  fruit  >>>set([‘o', ‘p', ‘a', ‘b'])  'orange' in fruit  >>>True

 Similar to hash tables.  A set of key:value pairs.  tel = {'jack': 4098, 'sape': 4139}  tel.keys()  >>> ['jack‘,’sape’]

 Retrieves keys and values from a dictionary.  knights = {'gallahad': 'the pure', 'robin': 'the brave'}  for k, v in knights.iteritems():  print k, v  >>>gallahad the pure robin the brave

 While looping over a sequence, provides the position of elements.  for i, v in enumerate(['tic', 'tac', 'toe']):  print i, v  >>>0 tic  >>>1 tac  >>>2 toe

 Zip(list1, list2) : loops 2 or possibly more sequences at the same time.  Reversed(list) : loops over the list in a reverse order.  Sorted(list) : returns the list in a sorted order, however leaves the original list unsorted.