12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

As suggested near the end of Table 8-2, the last form is also commonly used in conjunctionwith the zip function, to combine separate lists of keys and values obtaineddynamically at runtime (parsed out of a data file’s columns, for instance).Provided all the key’s values are the same, you can also initialize a dictionary withthis special form—simply pass in a list of keys and an initial value for all of them (thedefault is None):>>> dict.fromkeys(['a', 'b'], 0){'a': 0, 'b': 0}Although you could get by with just literals and key assignments at this point in your<strong>Python</strong> career, you’ll probably find uses for all of these dictionary-creation forms asyou start applying them in realistic, flexible, and dynamic <strong>Python</strong> programs.Chapter SummaryIn this chapter, we explored the list and dictionary types—probably the two mostcommon, flexible, and powerful collection types you will see and use in <strong>Python</strong> code.We learned that the list type supports positionally ordered collections of arbitraryobjects, and that it may be freely nested, grown and shrunk on demand, and more.The dictionary type is similar, but it stores items by key instead of by position, anddoes not maintain any reliable left-to-right order among its items. Both lists and dictionariesare mutable, and so support a variety of in-place change operations notavailable for strings: for example, lists can be grown by append calls, and dictionariesby assignment to new keys.In the next chapter, we will wrap up our in-depth core object type tour by looking attuples and files. After that, we’ll move on to statements that code the logic that processesour objects, taking us another step toward writing complete programs. Beforewe tackle those topics, though, here are some chapter quiz questions to review.170 | Chapter 8: Lists and Dictionaries

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!