-3

In the below list I'm trying to insert each name and phone number in a db. So I tried to iterate and assign it to a variable and use it in insert statement.

contactlist = [
  ['Siemens, Harper',  '323-4149'],
  ['Smith, Patti',  '239-1212'],
  ['Jackson, Janet',   '313-1352'],
  ['Manfredi, Ralph','872-2221'],
  ['Thompson, Bobby',   '365-2622'],
  ['James, Lebron',  '457-6223'],
  ['Ziegler, Zig',   '667-1101'],
  ['Robbins, Tony', '329-2310']
]

This is what I have been trying:

a = []
for data in contactlist:
    #print (data)
    print (data[0])
    for d1 in data:
        #print (d1)
        a.append(d1)
print (a)
Torbjörn
  • 5,512
  • 7
  • 46
  • 73
Varun
  • 117
  • 1
  • 4
  • 14
  • 3
    Possible duplicate of [Making a flat list out of list of lists in Python](https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python) – Turn Jul 31 '17 at 05:33
  • which `db` you are referring? – Rahul Jul 31 '17 at 05:36

4 Answers4

1

you can use reduce function from functools

from functools import reduce    
reduce(lambda m,n: m+n,contanctlist)

in your code

>>> contactlist = [
...   ['Siemens, Harper',  '323-4149'],
...   ['Smith, Patti',  '239-1212'],
...   ['Jackson, Janet',   '313-1352'],
...   ['Manfredi, Ralph','872-2221'],
...   ['Thompson, Bobby',   '365-2622'],
...   ['James, Lebron',  '457-6223'],
...   ['Ziegler, Zig',   '667-1101'],
...   ['Robbins, Tony', '329-2310']
... ]
>>> a = reduce(lambda m,n: m+n,contactlist)
>>> a
['Siemens, Harper', '323-4149', 'Smith, Patti', '239-1212', 'Jackson, Janet', '313-1352', 'Manfredi, Ralph', '872-2221', 'Thompson, Bobby', '365-2622', 'James, Lebron', '457-6223', 'Ziegler, Zig', '667-1101', 'Robbins, Tony', '329-2310']

As per Varun your comment

contactlist = [
  ['Siemens, Harper',  '323-4149'],
  ['Smith, Patti',  '239-1212'],
  ['Jackson, Janet',   '313-1352'],
  ['Manfredi, Ralph','872-2221'],
  ['Thompson, Bobby',   '365-2622'],
  ['James, Lebron',  '457-6223'],
  ['Ziegler, Zig',   '667-1101'],
  ['Robbins, Tony', '329-2310']
 ]

phone_list = []
person  = []

for contact in contactlist:
    phone_list.append(contact[1])
    person.append(contact[0])

print phone_list
print person   
Kallz
  • 3,244
  • 1
  • 20
  • 38
0

As far as I can see you are accessing your numbers wrong. Your contactlist is a list of lists. So you can access each entry like the way you did. If you need the number take the second entry of that data like this:

for data in contactlist:
    name = data[0]
    number = data[1]
    print 'Name: %s, Number %s' %(name, number)

Instead of printing the entries you can do whatever you need to do with your variables to add them into your database.

Simon
  • 113
  • 1
  • 7
0

Looking at this: Inserting multiple rows in a single SQL query?, one way would be to create a string that you insert in your command

contactlist = [
  ['Siemens, Harper',  '323-4149'],
  ['Smith, Patti',  '239-1212'],
  ['Jackson, Janet',   '313-1352'],
  ['Manfredi, Ralph','872-2221'],
  ['Thompson, Bobby',   '365-2622'],
  ['James, Lebron',  '457-6223'],
  ['Ziegler, Zig',   '667-1101'],
  ['Robbins, Tony', '329-2310']
]

str([tuple(i) for i in contactlist])[1:-1]

Prints

"('Siemens, Harper', '323-4149'), ('Smith, Patti', '239-1212'), ('Jackson, Janet', '313-1352'), ('Manfredi, Ralph', '872-2221'), ('Thompson, Bobby', '365-2622'), ('James, Lebron', '457-6223'), ('Ziegler, Zig', '667-1101'), ('Robbins, Tony', '329-2310')"

But looking at the Python docs that is not necessary. Consider this full example: (https://docs.python.org/2/library/sqlite3.html):

import sqlite3

contactlist = [
      ['Siemens, Harper',  '323-4149'],
      ['Smith, Patti',  '239-1212'],
      ['Jackson, Janet',   '313-1352'],
      ['Manfredi, Ralph','872-2221'],
      ['Thompson, Bobby',   '365-2622'],
      ['James, Lebron',  '457-6223'],
      ['Ziegler, Zig',   '667-1101'],
      ['Robbins, Tony', '329-2310']
    ]

conn = sqlite3.connect('example.db')

c = conn.cursor()

# Delete table
c.execute('drop table if exists employees')

# Create table
c.execute('''CREATE TABLE employees
             (name, number)''')

# Insert a row of data
c.executemany('INSERT INTO employees VALUES (?,?)', contactlist)
# or
# c.execute("INSERT INTO employees VALUES {}".format(str([tuple(i) for i in contactlist])[1:-1]))

conn.commit() # Commit the changes
conn.close()

The database now contains a table called employees with the following rows:

('Siemens, Harper', '323-4149')
('Smith, Patti', '239-1212')
('Jackson, Janet', '313-1352')
('Manfredi, Ralph', '872-2221')
('Thompson, Bobby', '365-2622')
('James, Lebron', '457-6223')
('Ziegler, Zig', '667-1101')
('Robbins, Tony', '329-2310')
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

Just a suggestion but in your comment of Kallz's post you said "The same output Im getting in my code posted above also but my problem i want to extract individual names and phone numbers".

For this you might want to use a dictionary.

Contacts={}
Contacts['Barry']="184788174"
Contacts['Stewart']="1515174"
Contacts['Sarah']="1252358174"

print(Contacts['Barry'])

Returns:

184788174

Or to display all:

Contacts={}
Contacts['Barry']="184788174"
Contacts['Stewart']="1515174"
Contacts['Sarah']="1252358174"


for data in Contacts:
    print(data, ":")
    print(Contacts[data], """
""")

Returns:

Barry :
184788174 

Stewart :
1515174 

Sarah :
1252358174