Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Monday, July 16, 2018

How to convert from a LIST to a string in Python?

Methods of conversion are needed in every language.

You can convert a list to string as shown here for a simple list:



ListToString_1

Here is an example of conversion of a more complicated list from an earlier post:


ListToString_2

Saturday, July 14, 2018

How do you convert from Python to JSON in Python 3.70b2?

You should import json and use dumps() as shown.

Here data is a python list and it will be converted to json.

json_0

If you want to print it pretty you could do  like this here.

json_1

We started with a sorted list what if we did not, as in data2



json_2

Tuesday, July 10, 2018

How do you convert a List to a Tuple and vice-versa in Python?

You can easily convert from one to the other using the list() and tuple() as shown here.

Here is how from List to Tuple:



And, here is how from Tuple to List:

You can get a hint from Python 3.7.0b2 (v3.7.0b2:b0ef5c979b, Feb 28 2018, 01:32:48) [MSC v.1912 32 bit (Intel)] on win32

as shown here:



Saturday, June 9, 2018

What is List Comprehension in Python?

Python supports list comrpehension. List Comprehension is a cool way to create a list (based on list construct). The examples are run on Python 3.7.

Here is an excerpt from Wikipedia:

"A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) as distinct from the use of map and filter functions."

You can create a list in Python using a for loop:


ListComprehension_0

Python supports List comprehension and you can create the same list as shown here:


ListComprehension_1

Don't you think this is far easier. It turns out it is more efficient as well since you avoid append().

You can do this kind of operation as well.


ListComprehension_2


You can filter the list in list comprehension also:

ListComprehension_3


Friday, June 1, 2018

How do you construct a Python dictionary?

Python dictionary has features common to JSON values.


Python dictionary has keys and associated with keys there are lists.
{key:[values],key[values]..}

Here is an example:

graph={"A":["C","B"],
       "B":["A","C"],
       "C":["A","B","D","E"],
       "D":["C","E"],
       "E":["C","D"]
      }


Defining a dictionary in Python.


Dict_0


In Python you can do the following to access keys and associated lists.

You can find the keys as show here:


Dict_1

You can gather all of the items in dictionary using the following:

Dict_2

You can list out the values only as shown here:


Dict_3

You can also access the lists by looping as in the following:


Dict_4


More in my next...

Friday, March 30, 2018

How do you define an Array in Python?

Looks like a trick question.

There is no Array in Python but an object List exists which can be used to the same end.
  • The List may contain different list items or same kind of list items.
  • The List is indexed and zero based. The first list item has an index 0.
  • You can retrieve list items using their index. Even negative index is OK.
As defined here lst is a list .

lst=["a", "b", "c", "e"]

The list can have strings like this as well:

lst2=['a', 'b', 'c', 'e']

They both evaluate to the same: ['a', 'b', 'c', 'e']


List may contain different types of List Items as in this definition.


 lst2=["ab", 8, True, 5.8]

Retrieving list items using index
>>> lst[0]
'a'
>>> lst[-1]
'e'
>>> lst2[-2]
True
>>> lst[5]
Traceback (most recent call last):
  File "", line 1, in
IndexError: list index out of range

Negative Index
>>> lst[-1]
'e'
>>> lst2[-2]
True



These evaluations were carried out using Python 3.6.2